I have the following code in a controller:
$this->viewdata->scripts = array(
'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
'jstree/jquery.jstree-1.0-rc2.js',
'jquery.hotkeys-0.8.js',
'tags/index.js'
);
$this->viewdata->styles = array(
'reset.css',
'tags.css'
);
In my view I have a foreach
to wrap each of the listed javascript/css files into the respective HTML tag. I'm using a master template/layout approach; I just populate any/all fields in the master template and I end up with my web page. So, it's good for me to have my javascript/css all set up in the controller so my <script>
s and <link>
s can be in the <head>
, where they belong.
However, it dawned on me recently that the javascript and css do not belong in the controller because the view that's rendered is subject to change and may have very different javascript/css requirements.
I basically want to keep just one master template for maintainability but to also remove all reference to view/javascript/css from the controllers. What's out there to help me do this?
Thanks.
I dont use CI, but i regularly use Zend and Symfony. In both these frameworks there is some sort of asset helper you can call from the view to add css or js files to the stack. You then place a call to similar helper to output those files in your head. for example in symfony:
View:
<?php use_stylesheet('/css/mycss.css'); ?>
<?php use_stylesheet('/css/components.css'); ?>
<?php use_javascript('/js/site.js'); ?>
Layout:
<head>
<?php include_javascripts(); ?>
<?php include_stylesheets(); ?>
</head>
I would think something like this would be relatively easy to implement in CI if not present out of the box...