CCode for Artists - Miles DeCoster

Custom scripts or styles in a common page header file.

PHP includes are a great way to reuse common page parts like headers and footers. Since the header may also include links to scripts or stylesheets, it would also be nice to change or add those as necessary. To do that we assign the appropriate info to PHP variables in each file that includes the header and use the same variables in the header file to assign the variable to the script and link tags.

This uses a custom style from the added stylesheet.

In the content file:

<?php $customStyle="customStyles1.css"; $customScript="customScripts1.js"; ?>
<?php include('cfa-header.php'); ?>
In the cfa-header.php file:
<link href="<?php echo($customStyle); ?>" rel="stylesheet" type="text/css" media="screen">
<script type="text/javascript" src="http://codeforartists.com/js/<?php echo($customScript); ?>"></script>

For the sake of elegance we could also make sure the variables have a value set before calling them as in:

<?php
if (isset($customStyle)) {
	echo('<link href="css/'.$customStyle.'" rel="stylesheet" type="text/css" media="screen">');
}
?>
<?php
if (isset($customScript)) {
	echo('<script type="text/javascript" src="http://codeforartists.com/js/'.$customScript.'"></script>');
}
?>