Welcome Guest, Not a member yet? Register   Sign In
Javascript files in template
#1

[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>
<head>
<title>My Codeigniter Site</title>
</head>
<body>
<div id="nav">
<p>Nav list here</p>
</div>

<div id="content">
<php $this->load->view($mainView); ?&gt;
</div>

<div id="footer">
<p>Copyright info</p>
</div>
&lt;/body&gt;
&lt;/html&gt;

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

[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 Smile With carabiner, your &lt;head&gt; sectiopn would be something like this:
Code:
&lt;head&gt;
&lt;title&gt;My Codeigniter Site&lt;/title&gt;
&lt;?php $this->carabiner->display(); ?&gt;
&lt;/head&gt;

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');
That would add the foo.js link tag to be displayed with $this->carabiner->display() in the view.
#3

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

[eluser]cahva[/eluser]
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. 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">
&lt;?php echo $maincontent ?&gt;
</div>

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
$data['maincontent'] = $this->load->view('someview',$some_other_data, TRUE);

// Finally output the template
$this->load->view('maintemplate',$data);

As you can see. Simple Smile 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().
#5

[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);
$this->load->view('template');
rather then:
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);
$this->load->view('header');
$this->load->view('[insert content view here]');
$this->load->view('footer');
#6

[eluser]vertmonkee[/eluser]
Thanks for the replies and extra tips.

I shall Carabiner and theCloset suggestions and let you know how i get on.
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB