Download EE 2 Docs Download EE 1 Docs
ExpressionEngine

2.8.1 User Guide

Conditional Global Variables

Conditional Variables are pair type variables, which allow you to conditionally show or hide information on your pages based on criteria being met. Conditional variables have the same syntax available in PHP conditionals, which normally follows this form:

{if variable comparison-operator value}  Data between the tags that gets shown if the condition is met.  {/if}

Simple Conditionals

A conditional is considered “simple” if it is evaluating variables that are already available by the time the template parsing engine reaches the simple conditionals parsing stage. The expression evaluates a single variable (i.e. contains no logical operators such as OR, AND), and the conditional does not make use of the else or elseif control structures. In short, a simple conditional will look very much like this:

{if embed:name == "joe"}  <h1>Info about Joe!</h1>  {/if}

Currently the variables available for simple conditionals are:

Variable Type Example
Globals {app_version}
Snippets {snp_name}
Segments {segment_1}
Site {site_label}
Embed {embed:name}
Layout {layout:name}

Note

If you are testing against a word, you should enclose the word in single or double-quotes. If you are testing against a number, then you do not need to use quotes.

Note

The proscription against elseif applies to any conditionals inside the conditional tag pair.

Protected Characters

ExpressionEngine uses PHP to evaluate conditionals, and there are certain characters that must be protected in a variable so that they are not treated as PHP code. You should converting those characters into HTML entities or removing the character entirely. Here is the conversion list:

To evaluate Use instead
&#34;
\ &#92;
$ &#36;
( &#40;
) &#41;
{ remove
} remove
\n (linebreaks) remove
\r (carriage return) remove

If you are comparing a variable against a value that might include parentheses, you should use the HTML entities for parentheses listed in the table above instead. For example, if you want the conditional to evaluate whether the screen name is John Smith (Owner), you would write the conditional like so:

{if screen_name == 'John Smith &#40;Owner&#41;'}

Advanced Conditionals

Any conditional that isn’t a simple conditional is considered an “advanced” conditional and is evaluated much later in the template parsing order. More advanced conditionals can use logical operators (ex: OR, AND) to compare multiple variables to multiple values. Consider this example:

{if username == "Joe" OR username == "Bob"}
    <h1>Hey, Joe or Bob!</h1>
{/if}

Or this example:

{if username != "Joe" AND username != "Bob"}
    <h1>Hey, people who are neither Joe nor Bob!</h1>
{/if}

Else and Elseif

You can use two additional control structures to help tailor your results:

{if:elseif}

And:

{if:else}

These work similar to standard PHP else and elseif constructs. Here is an example:

{if username == "Joe"}
        <h1>Hey, Joe! Where were you Tuesday?</h1>
{if:elseif username == "Bob"}
        <h1>Hey, Bob! Thanks for the tickets!</h1>
{if:else}
        <h1>Welcome to our site.</h1>
{/if}

In the above example, if the currently logged in user has the username of “joe” he receives the first message. If not, EE evaluates the second conditional for the username of “bob”. If the username is neither joe nor bob a default message is shown.

Note

Don’t be confused by the {if: prefix. This simply helps the parsing engine identify each control structure. The information to the right of the prefix is what determines which conditional you are using.

Operators

The following comparison and logical operators are allowed within conditionals:

Comparison Operators

You can use any of the following operators to compare a variable to a value:

Operator Name
== Equal
!= Not Equal
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
<> Not equal
% Remainder of one number divided by another

Note

When comparing equality make sure to use two equal signs rather than one (e.g. ==).

Logical Operators

You can use the following operators to compare multiple variables to multiple values:

Operator Name Result
&& And TRUE if both conditions are TRUE.
|| Or TRUE if either condition is TRUE.
AND And TRUE if both conditions are TRUE.
XOR Xor TRUE if either condition is TRUE, but not both.
OR Or TRUE if either condition is TRUE.

Logical operators have a precedence that determines in what order the parts of a conditional are parsed. In the following advanced conditional the member_id and member_group parts of the conditional are compared first using &&, before their result is compared to the username part via OR.

{if member_id != '1' && member_group != "5" OR username == "Billy"} Hi! {/if}

So, if the member id of the site visitor is not 1 and their member group is not 5 and their username is Billy, they can view the data in the conditional. The table above lists the precedence of operators with the highest-precedence operators listed at the top of the table.

Modulus Operator

A modulus operator finds the remainder of division of one number by another. This can be handy when you want to do something every nth iteration of a loop. For example, if we want to display a horizontal rule for every 5th entry in a Channel Entries loop, we would write this conditional:

{if count % 5 == 0}
  <hr>
{/if}

This works because the remainder of 5 divided by 5 is 0.

Parentheses in Conditionals

Like PHP, you can use parentheses to group parts of a conditional together to have the part of the conditional between the parentheses evaluate before being compared to the part of the conditional outside the parentheses. For example, in the code below, the two member group parts are evaluated first before their result is compared to the channel_id part of the conditional:

{if (member_id == '1' OR member_id == '2') && channel_id == '5'}

So, if the member id of the visitor is either 1 or 2, and they are viewing the channel with id of 5, then they can see the contents of that conditional.

Short Conditionals

Certain conditionals exist in a shortened form in order to improve template readability. These conditionals are usually checking to see if a certain thing is true or exists:

Global Conditionals

if group_id

{if group_id == '7'}  You're an "Editor"!  {/if}

You can test against the Member Group. This tests the Member Group ID number. The alternate {if member_group == ‘3’} version of this conditional should be used inside of {exp:channel:entries} tags.

if logged_in

{if logged_in}  Welcome back to the site!<br /> <a href="{path='LOGOUT'}">Logout</a>  {/if}

This tag pair will display content within the pair if the person viewing the page is currently a logged in member.

Note

This only tests whether or not someone is logged in. If you want to restrict a particular page based on the member group assignment you’ll do that in your Template preferences in the Control Panel.

if logged_out

{if logged_out}  You aren't a member or aren't logged in.<br /> <a href="{path='member/login'}">Login</a>  | <a href="{path='member/register'}">Register</a>  {/if}

This tag pair will display content within the pair if the person viewing the page is not currently a logged in member.

You’ll notice in the “logout” link above that a special path is used: {path=’LOGOUT’}. This is a special-case path value that will automatically render the correct path for someone to log out.

if member_group

{if member_group == '7'}  You're an "Editor"!  {/if}

You can test against the Member Group. This tests the Member Group ID number. This variable/conditional is identical to the group_id one available above. {member_group} will work correctly inside a {exp:channel:entries} tag, however.

if member_id

{if member_id == '147'}  Ooh, you're really special, Frank!!  {/if}

Test for the member ID of the currently logged in user.

if screen_name

{if screen_name == "Mr. Ed"}  Thanks for all your hard work on the site, Ed!  {/if}

You can test against the screen name of the currently logged in user.

if total_comments

{if total_comments < 1}  What??  No one has commented on my site at all?!?!  {/if}

Test against the total number of comments submitted for the entire site.

if total_entries

{if total_entries > 1000}  Yowza!  This is one hot site!  {/if}

Test against the total number of entries submitted for the entire site.

if segment_*X*

{if segment_3 == "private"}  You're seeing something private!  {/if}

You can test against one of the URL Segments that are available. The conditional should be replaced with the correct segment name. e.g. if you’re interested in URL Segment 3, then use {if segment_3}.

if username

{if username == "elvira"}  Hi, mom!  I know it's you!  {/if}

You can test against the username of the currently logged in user.

Alternative Syntax

In order to be able to use some member variables in conditionals inside a channel entries tag, which processes its own member information, it is necessary to use an alternative syntax. All of the member variables may be used in conditionals with the addition of the prefix “logged_in_”.

{exp:channel:entries channel="default_site"}
        {if logged_in_member_id == author_id}
                <p>You wrote this entry!</p>
        {/if}
{/exp:channel:entries}

A list of the available member variables that utilize this alternate syntax follows:

  • logged_in_member_id
  • logged_in_group_id
  • logged_in_group_description
  • logged_in_username
  • logged_in_screen_name
  • logged_in_email
  • logged_in_ip_address
  • logged_in_location
  • logged_in_total_entries
  • logged_in_total_comments
  • logged_in_private_messages
  • logged_in_total_forum_posts
  • logged_in_total_forum_topics

User Contributed Notes

Posted by: Hop Studios on 27 November 2013 10:17pm
Hop Studios's avatar

if logged_in AND if logged_out can’t be used in a complex conditional (i.e. no AND or OR for you!)

Alternatively, use if logged_in_member_id > 0 , and if logged_in_member_id == 0 , both of which can be part of a complex conditional.

Posted by: johnniefp on 29 April 2013 5:52pm
johnniefp's avatar

@Hop_Studios
Isn’t in_group() part of Mo Variables?
http://devot-ee.com/add-ons/mo-variables

I hope you’re right and that it’s in the core where it belongs.

jfp

Posted by: Paul @ Westhouse IT on 4 February 2013 10:38am
Paul @ Westhouse IT's avatar

If you’re having difficulty with conditionals not evaluating properly don’t forget the quotes:

e.g.

{company_membership_package var_prefix="pkg"}
  {if 
'{pkg:title}' == 'Premium'}
    {snp_company
-listings_Premium}
  {
/if}
{
/company_membership_package} 

not

{company_membership_package var_prefix="pkg"}
  {if {pkg
:title} == 'Premium'}
    {snp_company
-listings_Premium}
  {
/if}
{
/company_membership_package} 

Single or double quotes seem to work so just try to be consistent with which one you use.

Posted by: Hop Studios on 27 June 2012 6:15pm
Hop Studios's avatar

You can also, apparently, do this, as of 2.5.2 and maybe earlier:

{if in_group('1|2|3')}
   links
{if
:else}
   no links
{
/if} 
Posted by: siffring on 7 January 2012 10:55am
siffring's avatar

Also note that this is parsed as a simple conditional.

{if segment_2 == "category"

While this is parsed as an advanced conditional.

{if "{segment_2}" == "category"
Posted by: jeromecoupe on 7 October 2011 9:47am
jeromecoupe's avatar

EE considers a conditional to be “Simple” IF AND ONLY IF:

* it contains no if/else sort of logic switching
* it does not use AND, OR, XOR, etc operators
* it ONLY works with segment, embed, or global variables

This thread initiated by John D Wells says it better than I could here and links to useful resources about conditionals and parse order in general.

Posted by: Florian Schroiff on 27 April 2011 5:29pm
Florian Schroiff's avatar
{comment}
Posted by: chealey on 13 August 2010 6:22pm
chealey's avatar

If you need to use the advanced conditional AND to check against multiple values BEFORE all EE variables are parsed you can put the conditionals inside each other ie:

{if segment_1 =="foo" && segment_2 =="bar"} something {/if} 
could be written like so in order to parse early
{if segment_1 == "foo"}{if segment_2 == "bar"} something {/if} {/if} 

Useful particularly if you need to test against multiple segments inside of {channel:entries} to determine parameters.

You must have an EllisLab product license and have at least 50 posts to the community forums to contribute notes to the User Guide