开发者

Inserting a { in an ExpressionEngine template file

I am trying to insert some analytics code into my ExpressionEngine template's footer files,开发者_C百科 but it treats the {}'s as a function call or something. Is there any way to make it so it understands that EE shouldn't execute what's inside the braces?

I've already tried inserting backslashes and it doesn't seem to work.

Any help would be much appreciated.


ExpressionEngine's Template Class parses curly braces {} as template variables.

Because many programming languages use curly braces, this can cause problems by ExpressionEngine replacing JavaScript curly braces as Template Variables.

For example, the following JavaScript with curly braces all on one line:

<script>var addthis_config = { 'ui_click': true };</script>

Will be parsed by ExpressionEngine as a template variable and rendered as:

<script>var addthis_config = ;</script>

You'll notice everything starting at the opening { and ending with the closing } curly brace gets parsed and replaced! As a workaround, you can place the braces on separate lines and avoid this problem:

<script>
    var addthis_config = {
        'ui_click': true,
        'data_track_clickback': true
    };
</script>

If you've written a JavaScript function that expects values from ExpressionEngine, just place your braces on separate lines — which is a good coding convention and is optimal for readability.


What is your Debug preference in EE? It should be set to "1" (recommended). If it's currently at "0" try changing the setting value to "1". In some cases there are possible issues with non-EE {} characters used while debug is set to "0".

You can change the Debug Preference from CP => Admin => System Administration => Output and Debugging => Debug Preference.

Putting the {} braces on separate lines would also work, but that Debug setting ("1") is highly recommended, and maybe even why this "bug" isn't fixed.


Separate your analytics code into a separate template.

It's probably because you have the analytics code INSIDE another EE loop and so it's trying to parse it as a template variable.

So isolate the code if you need it within the loop and create an embedded template to include.

Thus, create an include called .analytics.

In the .analytics template, do the following (I'm using Google Analytics as an example):

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

NOTE: Using this method, keep the template as a normal template, do NOT change it to a javascript template because you are using the <script type="text/javascript"> tags inside the template.

Then, in your main template, do a simple:

{embed="template_group/.analytics"}

And you will be good to go.


Try the Protect Javascript config variable. I've used it to mix/match EE vars and JS several times.

EE 1.x

$conf['protect_javascript'] = 'y';

Reference

EE 2

$config['protect_javascript'] = 'y';

Reference


You should be using the hidden config varable protect javascript

$config['protect_javascript'] = 'y';


Have you tried commenting out the whole block of Analtics code using EE template comment tags? i.e.

{!--

Your comments will go in here.
You can even span it across multiple lines.

--}

From here http://expressionengine.com/user_guide/templates/commenting.html


I recommend you to avoid inserting (or trying to insert) raw JS into HTML templates. You can create a different template, with type JavaScript instead of HTML, then you can either add it at the head with a script tag, or {embed="js/piece-of-raw-javascript"}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜