ExpressionEngine 使用 { } 括号渲染 JS 代码

发布于 2024-11-17 18:10:20 字数 229 浏览 0 评论 0原文

有没有办法强制表达式引擎不将大括号内的项目渲染为 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻之岛 2024-11-24 18:10:20

ExpressionEngine 的模板类将大括号{}解析为模板变量,寻找三种变量:单​​个变量、成对变量和条件变量:

// 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 的处理方式不同,并且会阻止高级条件解析器处理标记中的任何内容。例如,以下 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 函数,只需将大括号放在单独的行上 - 这是 良好的编码约定并且可读性最佳

<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' ;

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}

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:

<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>

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>

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>

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>

As suggested by Ben, you can change this behavior by setting a Hidden Configuration Variable: $conf['protect_javascript'] = 'n';

冷月断魂刀 2024-11-24 18:10:20

ExpressionEngine 的隐藏 $config['protect_javascript'] 实际上是做什么的?也许最好用一个例子来解释——请允许我来说明一下。

给定以下代码示例,使用 $config['protect_javascript'] = 'y'; 高级条件将完全被忽略:

<script>
    {if username == "admin"}
        Welcome, {username}!
    {if:elseif member_id == "2"}
        Welcome, {screen_name}!
    {if:else}
        Welcome, Guest!
    {/if}
</script>

这将产生以下输出:

<script>
    Welcome, admin!

    Welcome, Administrator!

    Welcome, Guest!
</script>

而当 $config[' protected_javascript'] = 'n'; 上面的相同代码片段将允许评估高级条件并生成:

<script>
    Welcome, admin!
</script>

如您所见,区别在于是否高级条件在 JavaScript 代码块中进行评估

简单条件模板标签 始终在

<script>
    // Simple Conditionals Are Unaffected and Always Work
    {if segment_2 != ""}
        {redirect="404"}
    {/if}
</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>
柠栀 2024-11-24 18:10:20

创建一个全局变量来包含您的 JS 代码。然后只需在模板中使用全局变量:

https://docs.expressionengine。 com/v2/templates/globals/index.html

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

ま昔日黯然 2024-11-24 18:10:20

由于某种原因,简单地将大括号放在单独的行上对我来说不起作用(我正在使用 EE v2.1.1)。
但是在大括号之前和之后放置注释行是有效的。例如我的 Google Analytics 代码:

<script>
(function(i,s,o,g,r,a,m)
//
{
//
i['GoogleAnalyticsObject']=r;i[r]=i[r]||function()
//
{
//
(i[r].q=i[r].q||[]).push(arguments)
//
}
//
,i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
//
}
//
)(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3636230-46', 'auto');
ga('send', 'pageview');
</script>

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:

<script>
(function(i,s,o,g,r,a,m)
//
{
//
i['GoogleAnalyticsObject']=r;i[r]=i[r]||function()
//
{
//
(i[r].q=i[r].q||[]).push(arguments)
//
}
//
,i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
//
}
//
)(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3636230-46', 'auto');
ga('send', 'pageview');
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文