在 ExpressionEngine 模板文件中插入 {
我试图将一些分析代码插入到我的 ExpressionEngine 模板的页脚文件中,但它将 {} 视为函数调用或其他内容。有什么方法可以使它理解 EE 不应该执行大括号内的内容吗?
我已经尝试过插入反斜杠,但似乎不起作用。
任何帮助将不胜感激。
I am trying to insert some analytics code into my ExpressionEngine template's footer files, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ExpressionEngine 的模板类 将花括号
{}
解析为 模板变量。由于许多编程语言使用大括号,因此 ExpressionEngine 替换 JavaScript 可能会导致问题大括号作为模板变量。
例如,以下 JavaScript 花括号全部在一行:
将由 ExpressionEngine 解析为模板变量并呈现为:
您会注意到一切都从开头
{
并以结束}
大括号结束,并被解析和替换! 作为解决方法,您可以将大括号放在单独的行上并避免此问题:如果您编写了一个需要来自 ExpressionEngine 的值的 JavaScript 函数,只需将大括号放在单独的行上 - 这是一个 < a href="http://javascript.crockford.com/code.html" rel="nofollow noreferrer">良好的编码约定并且是 可读性最佳。
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:
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.
您在 EE 中的调试偏好是什么?应将其设置为“1”(推荐)。如果当前为“0”,请尝试将设置值更改为“1”。在某些情况下,当调试设置为“0”时,使用非 EE {} 字符可能会出现问题。
您可以更改调试首选项
CP =>管理员 =>系统管理=>输出与调试=>调试首选项。
将 {} 大括号放在单独的行上也可以,但强烈建议使用“调试”设置(“1”),这甚至可能是此“错误”未修复的原因。
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.
将您的分析代码分离到单独的模板中。
这可能是因为您的分析代码位于另一个 EE 循环内,因此它尝试将其解析为模板变量。
因此,如果您需要在循环中使用代码,请隔离代码并创建要包含的嵌入式模板。
因此,创建一个名为 .analytics 的包含文件。
在 .analytics 模板中,执行以下操作(我使用 Google Analytics 作为示例):
注意:使用此方法时,请将模板保留为普通模板,不要将其更改为 javascript 模板,因为您正在使用
模板内的
标记。然后,在您的主模板中,执行一个简单的操作:
然后您就可以开始了。
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):
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:
And you will be good to go.
尝试保护 Javascript 配置变量。我曾多次使用它来混合/匹配 EE 变量和 JS。
EE 1.x
参考
EE 2
参考
Try the Protect Javascript config variable. I've used it to mix/match EE vars and JS several times.
EE 1.x
Reference
EE 2
Reference
您应该使用隐藏配置变量保护javascript
You should be using the hidden config varable protect javascript
您是否尝试过使用 EE 模板注释标签注释掉整个 Analtics 代码块?即
从这里 http://expressionengine.com/user_guide/templates/commenting.html
Have you tried commenting out the whole block of Analtics code using EE template comment tags? i.e.
From here http://expressionengine.com/user_guide/templates/commenting.html
我建议您避免将原始 JS 插入(或尝试插入)到 HTML 模板中。您可以创建一个不同的模板,类型为
JavaScript
而不是HTML
,然后您可以使用script 将其添加到
标签,或head
中{embed="js/piece-of-raw-javascript"}
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 ofHTML
, then you can either add it at thehead
with ascript
tag, or{embed="js/piece-of-raw-javascript"}