ExpressionEngine渲染带有{}括号的JS代码 [英] ExpressionEngine rendering JS code with { } brackets
问题描述
是否有一种方法可以强制表达式引擎不将大括号内的项目呈现为EE代码? Google图表工具使用包含大括号{}的javascript代码,自然而然EE认为这是一个变量并尝试呈现它.可以解决这个问题吗?
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?
推荐答案
ExpressionEngine的 Template类将花括号{}
解析为
ExpressionEngine's Template Class parses curly braces {}
as template variables, looking for three kinds of variables: single, pair, and conditional variables:
// Single Variable
{summary}
// Pair Variable
{category} ... {/category}
// Conditional Variable
{if segment_2 != ""} ... {/if}
CSS中的大括号被认为是一种特殊情况.
Curly braces in CSS are considered a special condition.
例如,以下CSS可以放置在模板中的任何位置,并可以智能地进行解析
For example, the following CSS is acceptable to place anywhere in a template, and gets intelligently parsed:
<style>
/* All On One Line = Okay */
p { margin-bottom: 1em; }
/* Indented and On Separate Lines = Also Okay */
p em {
font-style: italic;
}
/* EE Variables are Parsed and Replaced */
p.{site_name} {
text-decoration: none;
}
/* EE Code is Allowed and Parsed Appropriately */
{exp:channel:entries channel="channel_name" limit="1"}
li.{url_title} a {
color: #c00;
}
{/exp:channel:entries}
</style>
不幸的是,JavaScript的处理方式有所不同,并阻止了高级条件分析器处理标记中的任何内容.例如,以下带有括号全部的JavaScript :
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:
<script>var addthis_config = { 'ui_click': true };</script>
ExpressionEngine将其解析为模板变量,并呈现为:
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>
如果您编写了一个JavaScript函数,该函数需要ExpressionEngine的值,则只需将花括号放在单独的行上—这是良好的编码约定,并且对于可读性而言是最佳的:
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:
<script>
$(document).ready(function() {
...
{exp:channel:entries channel="channel_name" limit="1"}
var business_name = '{business_website}';
var business_website = '{business_website}';
{/exp:channel:entries}
...
});
</script>
按照Ben的建议,您可以通过设置隐藏配置变量:$conf['protect_javascript'] = 'n';
As suggested by Ben, you can change this behavior by setting a Hidden Configuration Variable: $conf['protect_javascript'] = 'n';
这篇关于ExpressionEngine渲染带有{}括号的JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!