Feed Syntax: Common Methods and Regular Expressions

Common Methods

For a complete list of virtual methods, read the VMethods section of the Template Toolkit manual.

join

This method joins the items in the list into a single string. This method should follow the format below where the value in parentheses is added between each list item being joined:

{ items.1 = Womens Shoes
items.2 = Boots
items.3 = Knee-High }
{ items.join(' > ') }

The sample above provides the following output:

Womens Shoes > Boots > Knee-High

lower

This method returns the specified text in lowercase.

{ product.title = The Wonderful World of Parks and Recreation }
{ product.title.lower }

The sample above provides the following output:

the wonderful world of parks and recreation

remove

This method outputs the string with all instances of the pattern--specified in the parentheses--removed. The pattern used must be a regular expression (for more on regular expressions, jump to the Common Regular Expressions section). The sample below uses the regular expression \W+ to remove one or more non-word characters:

{ name = Barry, Barry & Sons }
{ name.remove('\W+') }

The sample above provides the following output:

BarryBarrySons

replace

This method outputs the string by searching for a pattern and replacing it with the specified text. The pattern used must be a regular expression. This method should follow the format below where the first value in parentheses is the pattern being searched for and the second value is what's replacing the pattern. The sample below uses the regular expression \W+ to replace one or more non-word characters with underscores:

{ name = Barry, Barry & Sons }
{ name.replace('\W+', '_') }

The sample above provides the following output:

Barry_Barry_Sons

trim

This method returns the text with any leading and trailing whitespace removed.

{ text = '  Spaces: The Final Frontier        ' }
{ text.trim }

The sample above provides the following output:

Spaces: The Final Frontier

Common Regular Expressions

You don't just have to match on fixed strings. Regular expressions, or regexes, allow you to match on just about anything. Look below for a list of the most common regexes. For more on regular expressions, read this Perl tutorial on regexes.

Regex Character
Function
.
a single character
\s
a whitespace character
\S
non-whitespace character
\d
a digit (0-9)
\D
a non-digit
\w
a word character
\W
a non-word character
[aeiou]
matches a single character in the given set
[^aeiou]
matches a single character outside the given set
(foo|bar|baz)
matches any of the alternatives specified
^
start of string
$
end of string

Quantifiers can be added to regular expressions to specify how many of the previous thing you want to match on, where "thing" means either a literal character, one of the metacharacters listed above, or a group of characters or metacharacters in parentheses.

Quantifier
Function
*
zero or more of the object
+
one or more of the object
?
zero or one of the object
{3}
matches exactly 3 instances of the object
{3,6}
matches between 3 and 6 instances of the object
{3,}
matches 3 or more instances of the object

You can combine different regex characters and quantifiers to create new regular expressions. Look below for some brief examples:

Regex
Function
\W*
zero or more non-word characters
^\d+
strings starts with one or more digits
^$
nothing in the string (start and end are adjacent)
(\d\s){3}
three digits, each followed by a whitespace character (e.g. "3 4 5 ")
(a.)+
matches a string in which every odd-numbered letter is 'a' (e.g. "abacadaf")
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk