Learn

Conditional Statements

Bring logic into your templates.

Statamic supports if, elseif, else, unless, and unlesselse conditional statements. Only valid & true conditions will be parsed, leaving you worry-free about page load demands.

{{ if }} blocks are closed with an {{ endif }}. As of v1.8, {{ if }} blocks can be closed with {{ /if }} instead of {{ endif }}.

Variables inside of conditionals should not use the tag braces. Using them will confuse the template parser and it will try to close your tag prematurely. For example, using the segment_1 variable in an if statement looks like this:

{{ if segment_1 }}

Conditionals can use any of PHP’s standard comparison operators:

  • ==
  • !=
  • ===
  • !==
  • >
  • <
  • >=
  • <=

…as well as PHP’s logical operators:

  • !
  • ||
  • &&
  • and
  • or

Checking if a Variable Exists

Statamic will return null for any variable that doesn’t exist. This lets you to use shorthand syntax (no comparison operator) to check for a variable:

{{ if bears }}
   Bears? Oh we've got bears.
{{ endif }}

Here’s a more complex example:

{{ if featured_article == "yes" }}
   <div class="featured sticky">
      <h1>{{ title }}</h1>
      {{ content }}
   </div>
{{ endif }}

{{ if bear == "grizzly" }}
   <p>Brace yourself, son. It's on.</p>
{{ elseif bear == "koala" }}
   <p>It's okay to weep openly. They're adorable.</p>
{{ else }}
   <p>At least it's a bear, I'll give you that.</p>
{{ endif }}

Combining with Variable Modifiers

Variable modifiers let you change the value of a variable on the fly. There are a couple of modifiers that check for variable conditions and will return a true-or-false answer that can also be used within conditionals.

For example, the in_future modifier checks to see if a variable’s value is a date that’s in the future. Let’s assume that {{ party_start_date }} is text containing the starting time and date of our party. Using it in a conditional statement, we can check to see if things have started or not:

{{ if party_start_date|in_future }}
    You still have time to RSVP!
{{ else }}
    Sorry, our socks have already been partied off.
{{ endif }}

You can also use variable modifiers and then use those in comparisons. For example:

{{ if segment_2 == my_variable|reverse|backspace:4|reverse }}
    <!--
    Will only be displayed if {{ segment_2 }} matches
    the value of {{ my_variable }} after its value has been reversed,
    with 4 characters removed from the end, and then
    reversed again.
    -->
{{ endif }}

If/Elseif/Else Shortcut or (added in v1.8)

There are times that you want to display one variable if it exists, or otherwise another one. To make this less cumbersome within your templates, if statements can check for more than one variables and display the one that exists by using the or shortcut.

Instead of doing this:

{{ if html_title }}
    {{ html_title }}
{{ else }}
    {{ title }}
{{ endif }}

You can do this:

{{ html_title or title }}

This will print the value of {{ html_title }} if it exists and isn’t false-y, otherwise it will print the value of {{ title }}.

You can also set a literal default, so instead of doing this:

{{ if html_title }}
    {{ html_title }}
{{ elseif title }}
    {{ title }}
{{ else }}
    No title was given.
{{ endif }}

You can do this:

{{ html_title or title or "No title was given." }}

You can even use modifiers if you’d like:

{{ html_title|upper or title|reverse|lower or "No title was given." }}

Using Regular Expressions (added in v1.8)

As of v1.8, you can compare variable values against regular expressions with the ~ operator.

{{ if age ~ "/\d{2,3}/" }}
    I see that you are {{ age }} years old.
{{ endif }}

A couple of notes on using regular expressions:

  • You need to use delimiters at the start and end of your regex, otherwise you’ll see an error
  • You can use flags at the end of your regex to modify its behavior

This article was last updated on September 13th, 2016. Find an error? Please let us know!