I am creating a web-based application in PHP. I have separated headers and footers and have created separate files for them "headers.php" && "footers.php". The "headers.php" file has all the styles in it and also the CSS files of different libraries and the same goes with the "footers.php" file, it has all the scripts in it.
My question is, is there a way where I can include all these different CSS and scripts depending upon the template.
here is an example:
if(template == 'somefilename.php'){
<script type="text/javascript" src="js/libraries/dataTables.bootstrap4.min.js"></script>
}
If I understand your question correct, you could do the following:
In the file, lets say home.php in that file you include or require your header.php and footer.php
// home.php
<?php
$addToHeader = '<script type="text/javascript" src="js/libraries/dataTables.bootstrap4.min.js"></script>';
require 'header.php';
// your html content
require footer.php;
?>
// header.php
// Your header code with your links etc...
<?php
if (isset($addToHeader)) {// Check if $addToHeader exist to prevent undefined errors
echo $addToHeader;
}
By doing something like this you can inject different links/scripts into your pages. The same can be done within your footer but then with an other variable name.