Expressionengine interpreting jQuery curly braces as EE tags
I'm using EE2.2 along with jQuery and jQuery templating plugin. In my template i have allot of curly braces that is part of how jQuery Templating works. The problem is EE is not differentiating between JavaScript curly braces and EE tags which also comes in curly braces. EE is interpreting the curly braces as EE tags and for that reason the template not working. Since these curly braces are inside a script tag, i would assume EE would count them as javascript and non EE tag related. How can i tell EE that these are not EE tags. Is there a work around this.
<script id="template-download" type="text/x-jquery-tmpl">
<tr class="template-download{{if error}} ui-state-error{{/if开发者_运维问答}}">
{{if error}}
<td></td>
<td class="name">${name}</td>
<td class="size">${sizef}</td>
<td class="error" colspan="2">Error:
{{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
{{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
{{else}}${error}
{{/if}}
</td>
{{else}}
<td class="preview">
.....
</script>
What does ExpressionEngine's hidden $config['protect_javascript']
actually do? It's probably best explained by an example — allow me to illustrate.
Given the following code sample, with $config['protect_javascript'] = 'y';
advanced conditionals will completely be ignored:
<script>
{if username == "admin"}
Welcome, {username}!
{if:elseif member_id == "2"}
Welcome, {screen_name}!
{if:else}
Welcome, Guest!
{/if}
</script>
Which will produce the following output:
<script>
Welcome, admin!
Welcome, Administrator!
Welcome, Guest!
</script>
Whereas, when $config['protect_javascript'] = 'n';
the same code snippet from above will allow advanced conditionals to be evaluated and will produce:
<script>
Welcome, admin!
</script>
As you can see, the difference is whether or not advanced conditionals are evaluated in JavaScript code blocks.
Simple conditionals and template tags are always evaluated in <script>
tags, regardless of the $config['protect_javascript']
setting — just be sure to place your curly braces {}
on separate lines!
<script>
// Simple Conditionals Are Unaffected and Always Work
{if segment_2 != ""}
{redirect="404"}
{/if}
</script>
This is a common question that has been answered before. Simply put:
- Place your JavaScript's curly braces
{}
on separate lines, or - Enact the
$config['protect_javascript']
Hidden Configuration Variable
With your usage, the second option is probably the best solution.
You can also set your template type to "static" for an easy fix. If your JS is in a template with other EE code, you can move it to a "static" template and embed it.
精彩评论