Expressionengine 将 jQuery 花括号解释为 EE 标签

发布于 2024-12-07 15:47:26 字数 971 浏览 0 评论 0原文

我正在使用 EE2.2 以及 jQuery 和 jQuery 模板插件。在我的模板中,我分配了花括号,这是 jQuery 模板工作原理的一部分。问题是 EE 没有区分 JavaScript 大括号和同样包含在大括号中的 EE 标签。 EE 将大括号解释为 EE 标签,因此模板无法正常工作。由于这些大括号位于 script 标记内,因此我假设 EE 会将它们视为 javascript 和非 EE 标记相关。我如何告诉 EE 这些不是 EE 标签。有解决这个问题的方法吗?

<script id="template-download" type="text/x-jquery-tmpl">
    <tr class="template-download{{if error}} ui-state-error{{/if}}">
        {{if error}}
            <td></td>
            <td class="name">${name}</td>
            <td class="size">${sizef}</td>
            <td class="error" colspan="2">Error:
                {{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
                {{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="preview">
             .....
</script>

I'm using EE2.2 along with jQuery and jQuery templating plugin. In my template i have allot of curly braces that is part of how jQuery Templating works. The problem is EE is not differentiating between JavaScript curly braces and EE tags which also comes in curly braces. EE is interpreting the curly braces as EE tags and for that reason the template not working. Since these curly braces are inside a script tag, i would assume EE would count them as javascript and non EE tag related. How can i tell EE that these are not EE tags. Is there a work around this.

<script id="template-download" type="text/x-jquery-tmpl">
    <tr class="template-download{{if error}} ui-state-error{{/if}}">
        {{if error}}
            <td></td>
            <td class="name">${name}</td>
            <td class="size">${sizef}</td>
            <td class="error" colspan="2">Error:
                {{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
                {{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="preview">
             .....
</script>

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

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

发布评论

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

评论(3

柠檬色的秋千 2024-12-14 15:47:26

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-12-14 15:47:26

这是一个常见问题,之前已得到解答。简而言之:

  • 将 JavaScript 的花括号 {} 放在单独的行上,或者
  • 执行 $config['protect_javascript'] 隐藏配置变量

根据您的使用情况,第二个选项可能是最好的解决方案。

This is a common question that has been answered before. Simply put:

With your usage, the second option is probably the best solution.

你げ笑在眉眼 2024-12-14 15:47:26

您还可以将模板类型设置为“静态”以便轻松修复。如果您的 JS 与其他 EE 代码位于模板中,您可以将其移动到“静态”模板并嵌入它。

You can also set your template type to "static" for an easy fix. If your JS is in a template with other EE code, you can move it to a "static" template and embed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文