Welcome Guest, Not a member yet? Register   Sign In
How to load selective JS and CSS in individual views
#1

[eluser]jaswinder_rana[/eluser]
Hi:

I have a view where I want to put a JS slideshow. I found a good one which uses MooTools and other files.

However, I only need it in one view. My views selectively load header and footer.
Code:
&lt;?php echo $this->load->view('header');?&gt; //Includes from &lt;html&gt; to <div> OPENING content tag
BODY
&lt;?php echo $this->load->view('footer');?&gt;from CLOSTING </div> content tag to &lt;/html&gt;

Now I may have misunderstood but I am assuming that all those &lt;head&gt; tags etc. should be views.

I could probably set an optional $tags variable and in my header check for it's existence. If it exists then output it.

As for the value of that tag, I can just supply the &lt;link&gt; and [removed] tag.

Am I doing this the wrong way?

Is there another way to do it in CI which I missed in the manual? Or something obvious which I am missing?

Thanks
#2

[eluser]umefarooq[/eluser]
Hi
use some template library it can solve your problem you can have different js and css for each and every view here is nice template library

Template Library

you can define regions just define two more regions like _scrips and _css and just put your all js and css file you want to put for any of your views.
#3

[eluser]missionsix[/eluser]
i have a variable in my header template called 'headinclude' where i can manually include special javascript / css views.


Works for me, and its pretty simple to do. But yeah, the template library is another option.
#4

[eluser]darkhouse[/eluser]
I run into this a lot, so I actually wrote a library (that I autoload) that allows me to manage js and css files per controller. I called it External.

Code:
//In my controller I can do this
$this->external->set_js('js/somefile.js');
$this->external->set_js('alert("testing");', 'custom');

//or I can do this
$this->external->set_js(array('js/somefile.js', 'js/someotherfile.js'));

//or I can even do this
$this->external->set_js(array(
     'js/somefile.js',
     'js/someotherfile.js',
     array('alert("testing");', 'custom')
));

And css is setup the same way, but with a few more options like for media="print", ie specific and ie6 specific.

Then in the header view, I just have this:

Code:
$this->external->run();

I just have one more feature to add to it, which is a config file for autoloading certain files so you don't have to put them directly in the view, or load them in every controller. I was also thinking about having groups setup in the config based on the uri string, so that if you were in, say, something like 'admin/whatever' and you had a group setup for 'admin', any controller in the admin would auto load those files. I normally have different css for my admin areas than for the front end.

I know there are template systems, but if anyone's interested in this, let me know and I'll send it once I have the config system done.
#5

[eluser]jaswinder_rana[/eluser]
This library sounds interesting.

Can you please share it? I am not sure how long it will take config system to work but if you can share what you have now, it'd be great (needed it now).

Right now I am doing as missionsix mentioned. I din't want to do this from controller so I am sending this data from view it self. It works nicely.

Thanks
#6

[eluser]darkhouse[/eluser]
Ok, I'm actually rewriting some stuff for it right now, I should be done shortly, I'll post it in Ignited Code when it's done and a link here.
#7

[eluser]jaswinder_rana[/eluser]
[quote author="darkhouse" date="1231110585"]Ok, I'm actually rewriting some stuff for it right now, I should be done shortly, I'll post it in Ignited Code when it's done and a link here.[/quote]
Thanks.Much appreciated.

And that proves that choosing CI as Framework was a good idea. Smile
#8

[eluser]darkhouse[/eluser]
[quote author="jaswinder_rana" date="1231110906"]And that proves that choosing CI as Framework was a good idea. Smile[/quote]

I don't know how I got by without CI before, and I can't see using anything else.
#9

[eluser]darkhouse[/eluser]
Here you go. I even finished the config file stuff. It's working really well.

http://ellislab.com/forums/viewthread/101236/

Enjoy.




Theme © iAndrew 2016 - Forum software by © MyBB