Help:TT Filters
HELP PAGE DISCUSSION CLOSE
Revision as of 23:21, 24 March 2013 by Matt (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Processing variables with filters

Filters are special functions you can apply to variables to further process the data stored in the variable. ucfirst for example, is a filter that changes the first letter of a string to an upper case letter. collapse is a filter that will change all the whitespaces that are more than a single character long into a single space character. Let's look at an example using a few of these filters.

[% mytext1 = "survey\n"; mytext1 FILTER ucfirst; mytext1 | ucfirst; %]

The example above will result in the output below:

Survey Survey

To apply a filter to a variable, you can use the format variable FILTER filtername or the shortened version variable | filtername. So in the example above, the filter ucfirst was applied to the variable mytext1, changing the first letter of the string to an upper case. It is important to note that the filters do not change the content of the variables, but instead, they change the output of the variables. Variable mytext1, prior to the application of the filter, held the string 'survey'. After ucfirst was applied, mytext1 still contained the string 'survey'.


Chaining filters in series

Filters can also be chained together, which allows you to apply multiple filters to a single variable. Here is an example of chaining the filters.

[% mytext2 = "This LINE is 32 characters LONG.\n"; mytext2 | lower | ucfirst | truncate(30); %]

The example above will result in the output below:

This line is 32 characters ...

The code above applies 3 different filters to the variable mytext2. The filters are applied in order from the left to right (first lower, then ucfirst, and finally truncate(30)). Filter lower converts all of the letters in mytext2 to lower case letters. Filter ucfirst makes the first letter of mytext2 an upper case letter. And finally, filter truncate(30) limits the string stored in mytext2 to 30 characters. If truncate(30) encounters a string that is longer than 30 characters long, it will take the first 27 characters, then append the string '...' (3 characters long) to the end, resulting in a final string of 30 characters long. For a full list of available filters Click Here.


Back to Table of Contents