« MySQL Links | Main | Where can one find the latest version of MySQL »

Smarty tip for including multiple javascript files

I was looking at an article from Yahoo's Cal Henderson article called Serving JavaScript Fast.

Previously one would have a good chunk of lines to include different javascript files based on what your form was requiring for effects, etc.:

<script src="/js/menu.js" type="text/javascript"></script>
{if $js eq 'formupload'}
<script src="/js/prototype.js" type="text/javascript"></script>
<script src="/js/effects.js" type="text/javascript"></script>
<script src="/js/upload.js" type="text/javascript"></script>
{elseif $js eq 'formuploadnewsimage'}

And in the template file where you include the header.tpl file you would specify which set of javascript files to also include:

{include file="header.tpl" js="formupload"}

I decided to spend a bit of time and actually get Cal's code to work from the example code (saved to plugins/insert.js.php), which I discovered that you can't invoke it from the example as:

{insert_js files="foo.js,bar.js,baz.js"}

I ended up having to do invoke it using the insert tag and specifying a name to be js so that it runs the smarty_insert_js function which is in the plugins/insert.js.php file mentioned before:

{insert name="js" files="foo.js,bar.js,baz.js"}


TrackBack URL for this entry:

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)