Include a JS or CSS file in a TPL

Sugar Dev Team —  November 23, 2012 — 4 Comments

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.

4 responses to Include a JS or CSS file in a TPL


    sugar_getjspathfile is not working.
    It says,
    “unrecognized tag: sugar_getjspathfile”


    Not works for me ..

Leave a Reply

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

You are commenting using your 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