解决方法
ExpressionEngine的Template Class将花括号{}解析为
template variables,寻找三种变量:单变量,对变量和条件变量:
// Single Variable {summary} // Pair Variable {category} ... {/category} // Conditional Variable {if segment_2 != ""} ... {/if}
CSS中的大括号被认为是一种特殊情况.
例如,可以将以下CSS放置在模板中的任何位置,并进行智能解析:
<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的处理方式不同,并阻止Advanced Conditionals Parser处理标记中的任何内容.例如,以下带有花括号的JavaScript全部在一行中:
<script>var addthis_config = { 'ui_click': true };</script>
将由ExpressionEngine解析为模板变量并呈现为:
<script>var addthis_config = ;</script>
你会注意到从开头{到结束}大括号结束的所有内容都会被解析和替换!作为解决方法,您可以将大括号放在单独的行上并避免此问题:
<script> var addthis_config = { 'ui_click': true,'data_track_clickback': true }; </script>
如果你编写了一个期望来自ExpressionEngine值的JavaScript函数,只需将你的大括号放在不同的行上 – 这是good coding convention并且是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的建议,您可以通过设置Hidden Configuration Variable来更改此行为:$conf [‘protect_javascript’] =’n’;