帮助使用 ExpressionEngine 模板标签和条件

发布于 2024-11-14 14:14:11 字数 780 浏览 3 评论 0原文

我接手了这个 EE2 项目,但对此没有太多经验。

我安装了两个插件,SessionVariables 和 Freebies,来获取 url 段并根据其值进行操作。

所以,基本上:

{if "{exp:freebie:any name="es"}" == "true"}
        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}
{if:else}
        {exp:session_variables:delete name="current_language"}
        Hello World!
{/if}

奇怪的是,当我访问 site.com/es/hello 时,它会打印“Hola Mundo”,但它实际上调用了 session_Variable 插件的函数 set() 和 delete() 。

我没有让我发疯,我很感激你能提供的任何帮助。

http://www.putyourlightson.net/projects/session_variables

https://github.com/averyvery/Freebie

I've taken on this EE2 project and I don't have much experience with it.

I've installed two plugins, SessionVariables and Freebies, to fetch a url segment and act according to its value.

So, basically:

{if "{exp:freebie:any name="es"}" == "true"}
        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}
{if:else}
        {exp:session_variables:delete name="current_language"}
        Hello World!
{/if}

The strange thing is that whan I go to site.com/es/hello it prints "Hola Mundo" but it actually calls both function set() and delete() of session_Variable's plugin.

I'ts driving me crazy, I'd appreciate any help you can provide.

http://www.putyourlightson.net/projects/session_variables

https://github.com/averyvery/Freebie

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

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

发布评论

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

评论(2

输什么也不输骨气 2024-11-21 14:14:12

当使用 EE 中的高级条件时,这些条件中的所有标签都会被解析,如下所示高级条件的解析时间晚于解析顺序中的大多数其他标记。这就是为什么在示例中的两个条件下都会调用 session_variables 标记(即使模板解析器仅显示内容的第一位)。

解决方法是使用“简单条件”,它的解析时间要早得多:

{if "{exp:freebie:any name="es"}" == "true"}
    Hola Mundo!
    {exp:session_variables:set name="current_language" value="_es"}
{/if}
{if "{exp:freebie:any name="es"}" == "false"}
    {exp:session_variables:delete name="current_language"}
    Hello World!
{/if}

When using advanced conditionals in EE, all tags within those conitionlas are parsed regardless, as advanced conditionals are parsed later than most other tags in the parse order. This is why the session_variables tag is being called within both conditions in your example (even though only the first bit of content is displayed by the template parser).

The fix is to use "simple conditionals", which are parsed much earlier:

{if "{exp:freebie:any name="es"}" == "true"}
    Hola Mundo!
    {exp:session_variables:set name="current_language" value="_es"}
{/if}
{if "{exp:freebie:any name="es"}" == "false"}
    {exp:session_variables:delete name="current_language"}
    Hello World!
{/if}
窗影残 2024-11-21 14:14:12

由于这个问题很早就出现在 Google 搜索中,我想我应该提到插件 IfElse。它将返回正确的条件,失败的条件(返回 false)将被删除。因此,仅运行所需的条件。

正如 Derek 所说,了解解析顺序至关重要,在这种情况下,无论条件是否有效,都会运行对 {exp:session_variables:...} 的调用。但是,使用 IfElse,您的代码将根据需要运行,同时避免高级条件,因此您不仅可以执行您想要的内容,而且可以在解析顺序启动之前更早地执行。

例如,

{exp:ifelse parse="inward"} 
{if "{exp:freebie:any name="es"}" == "true"}
        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}
{if:else}
        {exp:session_variables:delete name="current_language"}
        Hello World!
{/if}
{/exp:ifelse}

"{exp:freebie:any name="es"}" == "true"{exp:session_variables:set name="current_language" value="_es"}< /code> 将运行,不会 {exp:session_variables:delete...}。就好像您的代码如下所示:

        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}

另外,这将发生在 {exp:ifelse...} 的解析顺序上,而不是在高级条件之后。

需要注意的一个警告是您不能嵌套 {exp:ifelse...}

可能还有其他附加组件,例如 IfElse(查看同一作者的 Switchee),用于相同或相似的上下文,但这是我所知道和使用的。

Since this came up pretty early on a Google search I figure I'd mention the add-on IfElse. It'll return the proper condition, the ones that fail (return false) are stripped out. Ergo only the desired condition is run.

As Derek said knowledge of the parse order is vital, in this case both calls to {exp:session_variables:...} are being run regardless of the validity of the conditions. However, with IfElse your code would run as desired while avoiding advanced conditionals so not only do you execute what you want it does it much earlier in the parse order to boot.

E.g.

{exp:ifelse parse="inward"} 
{if "{exp:freebie:any name="es"}" == "true"}
        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}
{if:else}
        {exp:session_variables:delete name="current_language"}
        Hello World!
{/if}
{/exp:ifelse}

When "{exp:freebie:any name="es"}" == "true" then {exp:session_variables:set name="current_language" value="_es"} will run and not {exp:session_variables:delete...}. As if your code read:

        Hola Mundo!
        {exp:session_variables:set name="current_language" value="_es"}

Also, again, this will happen at the parse order of {exp:ifelse...}, not after advanced conditionals.

The one caveat to note is that you cannot nest {exp:ifelse...}.

There are probably other add-ons like IfElse (check out Switchee by the same author) for the same or similar contexts, but this is what I know and use.

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