Using Google Analytics Or Other Javascript With Smarty Template Engine



On a website I was working on recently I added the Google Analytics tracking code to the footer of a Smarty template, like this:

footer.tpl:



However, since the javascript used by Google Analytics includes { and } tags, also used by the Smarty template engine, it tries to interpret this code and depending on your settings will either fail silently or or with an error such as this:

Smarty error: [in footer.tpl line 148]: syntax error: unrecognized tag 'var'

The fix is simple. Enclose your Google Analytics code, or other javascript code, with {literal} and {/literal}. The literal tag allows you to place code to be displayed, well, literally.

The final code will look something like this:


{literal}

{/literal}



Your website should now run properly with the Google Analytics code in place.