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

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

Code:
$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:

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

Code:
<?=$css_bootstrap;?>
<?=$js_bootstrap;?>

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...
#2

[eluser]pzntec[/eluser]
I would suggest having an asset directory as such:
-application
-system
-asset
--css
--js
--img

Then within the head section of your html have:
Code:
<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:
Code:
RewriteCond $1 !^(index\.php|asset|robots\.txt)
*Notice the |asset|

So far this method has worked best for me.
#3

[eluser]timpiele[/eluser]
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

Code:
<?=$css_bootstrap;?>

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...
#4

[eluser]pzntec[/eluser]
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?

or

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.
#5

[eluser]timpiele[/eluser]
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

Code:
<?=$images['logo'];?>

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

Code:
<?=$css_bootstrap;?>

in the header rather than

Code:
<?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