Javascript files in template |
[eluser]vertmonkee[/eluser]
I am looking at suggestions for the best way to include different javascript files for different pages whilst still using a template. If I have a template view like this Code: <html> Where the variable mainView will refer to another view e.g. about.php or contact.php which is passed in from the controller. If I wanted to put a link to a javascript file in the head for the about page and then a different file for the contact page what would be the best way of doing this. Thanks
[eluser]cahva[/eluser]
Check out Carabiner. I can warmly recommend it. I've used it already on multiple projects and does what its supposed to: save time and make thing simple With carabiner, your <head> sectiopn would be something like this: Code: <head> That would print all css and javascript links and tags that you have configured. I myself use MY_Controller and load the default config in there and then in controller if I need a new javascript file, I add it in the controller: Code: $this->carabiner->js('foo.js');
[eluser]kilishan[/eluser]
You can always use asset management libraries, like Carabiner or my own theCloset. This allows you to change the files used on the fly and they can then be inserted anywhere on the page. According to Yahoo's Performance Rules, adding your JS just above the closing body tag is best for various reasons I won't go into here. Libraries like these make it easy to do this.
[eluser]cahva[/eluser]
Oh yeah just a little BTW The way you are now doing a template will bite you in the ass later. I did exactly the same thing many moons ago and loading the view inside a view(your main template) will make things harder when you need variables passed to the main view. And as I said harder not impossible but there is much simpler way to do that. Just replace the view load for example: Code: <div id="content"> In the controller then set the $maincontent with the view you want: Code: // Set the maincontent. Third parameter with TRUE will return it instead of printing it to browser As you can see. Simple In the way you now used a view inside a view and you would want to pass $some_other_data array to 'someview', you would have to pass it to mainview or use something like $this->load->vars().
[eluser]theprodigy[/eluser]
Quote:Oh yeah just a little BTW smile The way you are now doing a template will bite you in the ass later. I did exactly the same thing many moons ago and loading the view inside a view(your main template) will make things harder when you need variables passed to the main view. I have to slightly disagree with that statement. I use a template layout style, and load views in my templates all the time. I just use: Code: $this->load->vars($data); Code: $this->load->view('template', $data); Loading the vars, rather then passing them into the template makes them accessible to all views, no matter how nested you get. And you don't have to worry about passing them in with each view call. Similarly, if you do header, content, footer style, you could: Code: $this->load->vars($data);
[eluser]vertmonkee[/eluser]
Thanks for the replies and extra tips. I shall Carabiner and theCloset suggestions and let you know how i get on.
[eluser]vertmonkee[/eluser]
Having tried both the options out I think they are both excellent libraries. However I've decided to go with Carabiner for this project. Thanks for the help. |
Welcome Guest, Not a member yet? Register Sign In |