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 = [] items.1 = attr.Bullet_1 items.2 = attr.Bullet_2 } { items.join('</li> <li>') }
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.
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.
You can combine different regex characters and quantifiers to create new regular expressions. Look below for some brief examples:
Comments
0 comments
Please sign in to leave a comment.