Learn

Variable Modifiers

Changing your tag's values on the fly.

Ever have a variable that you want to display in all caps? Or have a decimal number in a variable that you want to round up to the closest whole number? You can do these things and more with Statamic’s variable modifiers.

play Watch our screencasts:
String Modifiers and Loop Modifiers

Modifiers let you modify your variable’s values. A simple modifier:

{{ warning|upper }}

The above will take the value of {{ warning }} and will display it in uppercase. It’s as simple as that. Note that there are no spaces between the tag name, the pipe, and the modifier. The template parser is strict here, you cannot use spaces in your modifiers.

Some modifiers let you specify parameters. For example, round lets you choose how many decimal places you’d like to round your numbers to — 0 by default.

Round to two decimal places:

{{ distance|round:2 }}

If the value of {{ distance }} was 3.1415, this would render as 3.14, rounding down to two decimal points.

Chaining Modifiers

Modifiers can be chained together, so that you can modify a variable multiple ways with one tag. For example, to convert a value to uppercase and then reverse the order of the string, you can do this:

{{ warning|upper|reverse }}

If the value of {{ warning }} is look out, the above tag will output TUO KOOL. Statamic starts at the left (with your variable’s current value), then applies each modifier left-to-right, down the chain. Each modification sticks along the way, letting you create chains as long as you’d like.

Another example, this time with math:

{{ angle|+:5|sqrt|cos }}

This will display the cosine of the square-root of your angle tag’s value plus five.

Checking for Conditions

There are a couple of modifiers that will check for a condition in your variable and change the value to either true or false for use in template conditionals. For example:

{{ event_start_date|in_future }}

The above code assumes that your {{ event_start_date }} tag is a date (or text that we can convert into a date). The in_future modifier lets you check to see if a given variable’s date value is in the future or not. This comes in handy when you need to check for such things in your template.

{{ if event_start_date|in_future }}
   {{# event hasn't started yet #}}
{{ else }}
   {{# event has already started #}}
{{ endif }}

Statamic doesn’t care how you use modifiers or in what order, so remember that these modifiers that check for a condition will work in a modifier chain, but probably aren’t something you will want to chain with. Why? Because when a condition is true (in the above example, if event_start_date is in the future), these modifiers change the value to the literal string true, and when false, convert it to an empty string. Chaining with those values doesn’t make a whole lot of sense.

More About Modifiers

There are over 50 different variable modifiers available: some that work best with text, some that work best with dates, some that work best with numbers, and others that do checks for conditions.

To learn more about what modifiers are available and how to use them, visit the variable modifiers reference in the documentation.

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