Is there a way to force expression engine to NOT render items within curly brackets as EE code? The google chart tools uses javascript code that contains curly { } brackets, and naturally EE thinks it's a variable and tries to render it. Any way around this?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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:Which will produce the following output:
Whereas, when
$config['protect_javascript'] = 'n';
the same code snippet from above will allow advanced conditionals to be evaluated and will produce: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!ExpressionEngine's Template Class parses curly braces
{}
as template variables, looking for three kinds of variables: single, pair, and conditional variables:Curly braces in CSS are considered a special condition.
For example, the following CSS is acceptable to place anywhere in a template, and gets intelligently parsed:
Unfortunately, JavaScript is handled differently and prevents the Advanced Conditionals Parser from processing anything in tags. For example, the following JavaScript with curly braces all on one line:
Will be parsed by ExpressionEngine as a template variable and rendered as:
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: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:
As suggested by Ben, you can change this behavior by setting a Hidden Configuration Variable:
$conf['protect_javascript'] = 'n';
Simply placing curly braces on separate lines didn't work for me for some reason (I'm using EE v2.1.1). But placing a commented line before and after the braces worked. E.g. for my Google Analytics code:
Create a global variable to contain your JS code. Then just use the global variable in your template:
https://docs.expressionengine.com/v2/templates/globals/index.html