Reputation: 1545
Quick and should be simple question, but i cant find the answer!!! So im trying to make a universal header called header.php. Now the only problem is that some pages have 5 css style sheets while others have only 2. And some pages have 5 js links in the header while some only have 1. How do i account for this variability in css and js links in the header? Am i suppose to use if statments? Variables? Thanks!
Upvotes: 3
Views: 1007
Reputation: 50029
You should be using a setup like this. Of course, you might want to make it so that the styles are included from your view and not your controller. Can't give a better answer without knowing your setup.
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; ?>
Upvotes: 1
Reputation: 831
Instead of creating a static header.php, create a function to include javascript files dynamically. For example, you need only jquery & jquery ui js references for page1, so you call
include_js('jquery', 'jquery-ui')
where include_js is your function which will insert respective JS files.
Similarly, in page2, you need assume you need jquery & jquery.fancybox
include_js('jquery', 'jquery.fancybox')
Upvotes: 1
Reputation: 22134
Loading all the CSS and JS file in every page won't matter much as after a first load, they will be cached and not retrieved again.
Therefore, you could just bundle them and not worry if you really need a particular file in the current page or not.
Upvotes: 0
Reputation: 14375
Consider combining resources to single ones. This will minimize HTTP requests which is a good practice.
Upvotes: 2