CodeIgniter Forums
javascript and template division - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: javascript and template division (/thread-33262.html)



javascript and template division - El Forum - 08-20-2010

[eluser]mysterious[/eluser]
I am developing an application with codigniter. I have divided my template into 5 files.

meta.php, header.php, maincontents.php, sidenavigation.php, footer.php

is this division right? Where do I write my javascript or jquery functions as different controllers has different functions and these functions include different jquery plugins.
If i write my scripts in meta.php then every page will load that script, that i think is a bad thing.

confused !!


javascript and template division - El Forum - 08-20-2010

[eluser]mddd[/eluser]
About the division: whatever works for you is right! I don't think there is any one "true solution".

About the scripts: I would put the functions in separate .js files. Then I would tell the 'meta' view (which probably means the 'head' part of your html document) which .js files to load. For instance by passing an array of js filenames.


javascript and template division - El Forum - 08-20-2010

[eluser]JoostV[/eluser]
In meta.php:

Code:
$js = array('jquery.js',
            'autocomplete.js',
            'lightbox.js');

foreach($js as $file) {
    echo '<script type="text/javascript" src="jscripts/' . $file . '"></script>' . "\n";
}

And yes, whatever works for you is right Smile


javascript and template division - El Forum - 08-20-2010

[eluser]mysterious[/eluser]
Thanks both of you .. thats what i was thinking but now i can do this with confidence. Smile