How to create php header with different css and js?

Quick and should be a simple question, but I can not find the answer !!! Therefore, I am trying to create a universal header.php header. Now the only problem is that some pages have 5 CSS stylesheets, while others have only 2. And some pages have 5 js links in the header, and some of them have only 1. How can I explain this variability in css and js links in title? Can I use if statuses? Variables? Thank!

+3
source share
4 answers

Consider combining resources into single ones. This minimizes HTTP requests, which are good practice .

+2
source

header.php javascript. , jquery jquery ui js 1,

include_js('jquery', 'jquery-ui')

include_js - , JS .

, . 2, , jquery jquery.fancybox

include_js('jquery', 'jquery.fancybox')

+1

You should use this setting. Of course, you can make styles be included in your view, and not in your controller. You can’t give a better answer without knowing the settings.

In your controller

$styles_for_layout = array();
$scripts_for_layout = array();

//and whenever you need to include a script in for your particular view
$scripts_for_layout[] = 'script_for_page.js';
$styles_for_layout [] = 'style_for_page.css';

headerp.php

<?php if(!empty($styles_for_layout))?>
  <?php foreach ($styles_for_layoutas $style) : ?>
     <link rel='stylesheet' href='<?php echo $style ?>'>
  <?php endforeach; ?>
<?php endif; ?>

<?php if(!empty($scripts_for_layout))?>
  <?php foreach ($scripts_for_layout as $script) : ?>
     <script type='text/javascript' src='<?php echo $script ?>'>
  <?php endforeach; ?>
<?php endif; ?>
+1
source

Downloading all CSS and JS files on each page does not matter, because after the first download they will be cached and not retrieved again.

Therefore, you can simply link them and not worry if you really need a specific file on the current page or not.

0
source

All Articles