Include a JS or CSS file in a TPL

bsoremsugar —  November 23, 2012 — 1 Comment

We had the question come up internally, “What is the best way to include an external javascript file in a Smarty template file”. This is very simple to do with the help of the sugar_getscript Smarty function as shown below…

Now the question you may ask here is, why not just include a normal script tag, like this…

The answer here is that the former option will add version bits to the URL, so that on an upgrade to a new version of Sugar, the new version file will be enabled to be used without having to clear the browser cache. And if you have developer mode turned on, the javascript file will never be cached by the browser, making your development cycle much easier without having the clear the browser cache constantly.

Using this method, the top code segment will render like this…

So be sure to use the sugar_getscript Smarty function when looking to include additional javascript files.

For CSS files, there isn’t a shortcut method like this, but you can leverage the Smarty function sugar_getjspath here to get the versioned URI string.

One response to Include a JS or CSS file in a TPL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s