2

Say I have a global variable foo. To output it in the templates, all I have to do is:

<h1>{foo}</h1>

However, I can't seem to use it in an if-condition. For example:

{if foo == 'bar'}<h1>Hello, world</h1>{/if}
{if {foo} == 'bar'}<h1>Hello, world</h1>{/if}
{if '{foo}' == 'bar'}<h1>Hello, world</h1>{/if}

How do I use a global variable in an if-condition?

3

The issue is one of parsing order... User Defined global variables are the last thing to be parsed.

This PDF outlines ExpressionEngine’s Parse Order.

This page in the docs details the Rendering Stages of the EE template engine.

The solution is to switch to using a Snippet instead.

2

You're running into a parse order issue here. The conditionals are parsed before the global variables, and therefore your comparison is taking place with the actual value you have within the conditional instead of the parsed value.

There are a couple things you can do.

1) Use a snippet instead of a global variable, as these parse much earlier

2) Use an add-on like Low Variables, which enables you to set global variables to be early-parsed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.