Welcome Guest, Not a member yet? Register   Sign In
What is the best way to implement the Asset Helper?

In the past I have always used the Asset Helper to populate variables from within each of my controllers like this:

$data['css_bootstrap'] = css_asset('bootstrap.css');
$data['js_bootstrap'] = js_asset('bootstrap.js');

and each of my controllers loads the header before the main view like this:

$this->load->view('header', $data);
and then just echo'd out those variables in my header like this:


but this requires me to repeat my code in every controller... (as well as include my header from each controller)

What is the most efficient way to just include these assets once so they are available in my header by default? I'd prefer not to place the calls to the asset helper in my header as I'm trying to keep it as PHP free as possible...

I would suggest having an asset directory as such:

Then within the head section of your html have:
<base href="<?php echo base_url().'asset/'; ?>" />

This way you would not have to reference each asset in your main html code.

Also, if you have an .htaccess file, have your asset directory ignored:
RewriteCond $1 !^(index\.php|asset|robots\.txt)
*Notice the |asset|

So far this method has worked best for me.

I do have an asset directory, maybe I should be more clear:

Since my CSS and JavaScript files are referenced in my header, which is a partial view, what is the best way to only have to reference them once for that partial?

Every time a view is loaded the header is there, and it has


then I have to pass a value to that variable from every controller, otherwise it is undefined...

Maybe just create one file with all of the asset calls and use include() at the top of my header? I hate to do that as I am trying to keep views clear of PHP...

I am a bit confused on how you have things setup.

Are you using some sort of template engine, are you using multiple themes/styles? Is that why you want to pass data into the view file?


Do you always call the same assets everytime? If this is the case, then using one header view file would be best hard coded with the assets would be your best bet.

Generally yes, I have one set of global assets I pass in (other than images)...

For images, I create an $images[] array on a per controller basis and pass that to the view and echo out the images like


to keep the code simple.

I generally use bootstrap, font awesome, jQuery and a few other libraries project-wide, so they need to be present in the header. I try to keep complex PHP out of my views because my designer has to work with them and it's easier for him to understand


in the header rather than

<?php echo css_asset('bootstrap.css');

My question is, other than hard coding calls to the asset helper in the header, is there an easier way (pattern) that you guys use for this, like putting the asset calls in a file and using include() to place it at the top of each controller's constructor, or even just including it in the header I guess?

You've got this block of like 8-10 calls to asset_helper, used on every page, but you don't want them in your header view file, where do you put them?

Theme © iAndrew 2016 - Forum software by © MyBB