User Tools

Site Tools


tutorials:using_javascript_in_smarty_templates:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorials:using_javascript_in_smarty_templates:start [2010/04/30 19:51]
matt created
tutorials:using_javascript_in_smarty_templates:start [2014/09/25 16:55] (current)
Line 1: Line 1:
 =====Using JavaScript inside SMARTY Templates===== =====Using JavaScript inside SMARTY Templates=====
 +
 +<tip c n>**Update for v6:** The software now uses SMARTY 3, which handles this a little differently. In short, SMARTY now ignores any { characters that are immediately followed by whitespace. allowing JavaScript to parse them correctly. The practical upshot of that is that **most** scripts will now work without the below modifications. If not, or if you're not sure, it's always safe to make these changes anyway.</tip>
  
 On occasion, it may be appropriate to add a block of JavaScript code to one of your template files. One of the most common uses of this is when adding Google Analytics to a site. There is one important caveat to be aware of: both JavaScript and SMARTY have different interpretations of the left-curly-brace character **'{'** -- if you try to insert JavaScript code containing {curly braces} into a SMARTY template file, SMARTY will attempt to interpret the braces as its own tags, which will usually break the JavaScript code. On occasion, it may be appropriate to add a block of JavaScript code to one of your template files. One of the most common uses of this is when adding Google Analytics to a site. There is one important caveat to be aware of: both JavaScript and SMARTY have different interpretations of the left-curly-brace character **'{'** -- if you try to insert JavaScript code containing {curly braces} into a SMARTY template file, SMARTY will attempt to interpret the braces as its own tags, which will usually break the JavaScript code.
  
-There are a couple of ways around this. You can either replace all of the left-curly-braces in your Javascript with the {ldelim} tag, or you can simply wrap the entire javascript code in {literal}{/literal} SMARTY tags.+There are a couple of ways around this. You can either replace all of the left-curly-braces in your Javascript with the {ldelim} tag, or you can simply wrap the entire javascript code in **{literal}{/literal}** SMARTY tags. 
 + 
 +<tip c n>**Google Analytics Tip:**  If you are trying to use Google Analytics:  Edit the **footer.tpl** template file in the admin panel under **Design > Manager**.  Click on the **<..> Source Code Editor** tab.  Right before the end **</body>** tag, copy/paste in the Google Analytics code as provided by Google Analytics.  Right before it, add **{literal}**, and right after the code you pasted in, add **{/literal}**.  See below for examples of what it might look like when you are done.</tip>
  
 We'll show the oft-offending piece of Google Analytics code here, by way of example. Google generally supplies the code like this: We'll show the oft-offending piece of Google Analytics code here, by way of example. Google generally supplies the code like this:
tutorials/using_javascript_in_smarty_templates/start.1272657086.txt.gz · Last modified: 2014/09/25 16:55 (external edit)