When serving up dynamic information, you want your pages to load as fast as possible. In ExpressionEngine (EE), not only pages, but also code such as CSS and javascript can be served using templates. But when should you use an EE javascript template? How does the use of a JavaScript template affect page load speed?
The choice whether or not to use javascript templates in EE can potentially have a huge effect on page load time in certain situations.
Javascript in EE Templates
In ExpressionEngine, you have 6 types of templates you can choose from. If you’re using a template to include javascript code in your pages, you’ll want to choose either JavaScript or Static.
If your javascript file is compressed with the whitespace removed (such as the minimized version of jQuery), EE will not process it properly unless you use the Static template type. Otherwise, you can just use the regular JavaScript template type.
Javascript as a Regular JS File
ExpressionEngine also allows you to just upload an external javascript file to the server and link to it just as you would any other file in HTML, PHP or what have you.
The downside is that you have to go outside of EE’s control panel and use FTP to load the file and then use FTP to edit it if need be later on. You won’t be able to make changes from within EE as you would if you were using a template.
Javascript Templates vs Static Files
First off, why would you want to use templates to display javascript in EE?
When putting javascript in a JavaScript template, you can use EE tags to pull in information from elsewhere in EE. This can be pretty handy at times; saving you from retyping extra info, or when information needs to change based on the page the javascript is being loaded. For these situations, using templates are great.
The catch with javascript in templates is that the code isn’t being cached. Each time a new page loads, EE treats the javascript template as a dynamic file and downloads it again; looking for EE tags it needs to process. Your browser doesn’t cache the javascript.
When dealing with larger javascript files—especially javascript frameworks, this can significantly increase your page load times.
Just linking from a page template to a regular old javascript file, on the other hand, means your browser can cache the file and then when a visitor goes to another page, wham-o, it’s already cached.
Speed Impact
I don’t have any benchmark tests for this, but from my personal experience, using javascript templates for serving multiple javascripts and/or large chunks of javascript code in EE can noticeably slow the loading of pages when compared to just linking to regular .js files. If you combine this with the time it takes to process the PHP and EE tags and the other templates for pages and pull in weblogs, this all adds up.
Recap
Using javascript templates in EE makes sense when:
- You need/want to use variables in your javascript
- Dealing with smaller javascript files
- You want to manage/organize/edit your javascript files through EE’s control panel
Serving javascript files as just regular old .js files makes sense when:
- You don’t need EE to parse anything in your javascript code
- You’re using a javascript framework such as Prototype, jQuery or MooTools
- You’re using several small javascript files
- Your pages are loading slower than you’d like


You should

6 Comments to 'Javascript Templates in EE: Speeding Up Page Loads'
November 28th, 2008
Isn’t there an option to Cache the javascript templates? Would that overcome the issue of js caching?
November 28th, 2008
@Jason L -
Good question.
EE does not cache javascript that’s inside a JavaScript template. The JS template allows it to contain EE tags within the javascript, so it’s treated as dynamic content. Turning on regular caching in the EE settings does not affect caching of javascript in Javascript templates.
If you want your javascript cached, it will need to be placed in an external .js file and linked to from a page template. This seems to be the preferred way of linking to javascript unless you’re needing to use some PHP/EE tags/conditionals within your javascript.
All in all, it mostly boils down to the fact that browsers can’t cache the JS template because they are being handled by EE itself.
Here’s a few posts on the EE Forums that addresses the question of javascript caching:
http://expressionengine.com/fo.....ead/87063/
http://expressionengine.com/fo.....ead/93663/ (Note Robin Sowell’s comments)
Note: CSS Stylesheet templates in EE, however, ARE cached by browsers.
February 24th, 2009
Hey, great post. Thanks for the hint! *deletes his jQuery templates*
October 12th, 2009
Really helpful thank you…
January 12th, 2010
I just noticed a benefit of putting Javascript as a template. I saved a small javascript file as a template and it gets compressed with GZIP (if you turn it on in the settings). The size shrinks from 11K to 3K.
So with template Javascript you have the benefit of using EE tags and smaller filesize and with external Javascript you have the benefit of caching.
I’m a bit confused now what to use.
January 12th, 2010
@Tilo – Interesting. I’ll have to test this.
As far as which you should use, I guess it depends a bit on your hosting server as far as which will give you the edge on speed.
I’d recommend running some tests and see for yourself which seems to load faster.
Subscribe to the Comments RSS feed—Follow the discussion.
Leave a comment
Note: All links in the comments are set to 'nofollow'. Search engines will not follow or index those links.
Follow this post's comments by subscribing to the Comments RSS feed.