The following is the list of builtin virtual methods and filters
that can be called on scalar data types. In Alloy and TT3, filters and
virtual methods are more closely related than in TT2. In general anywhere a
virtual method can be used a filter can be used also - and likewise all
scalar virtual methods can be used as filters.
In addition to the filters listed below, Alloy will automatically
load Template::Filters and use them if Template::Toolkit is installed.
In addition to the scalar virtual methods, any scalar will be
automatically converted to a single item list if a list virtual method is
called on it.
Scalar virtual methods are also available through the
"Text" virtual object (except for true filters such as eval and
redirect).
All scalar virtual methods are available as top level functions as
well. This is not true of TT2. In Template::Alloy the following are
equivalent:
You may set VMETHOD_FUNCTIONS to 0 to disable this behavior.
- '0'
-
[% item = 'foo' %][% item.0 %] Returns foo.
Allows for scalars to mask as arrays (scalars already will,
but this allows for more direct access).
Not available in TT.
- abs
-
[% -1.abs %] Returns the absolute value
- atan2
-
[% pi = 4 * 1.atan2(1) %]
Returns the arctangent. The item itself represents Y, the
passed argument represents X.
Not available in TT - available in HTML::Template::Expr.
- chunk
-
[% item.chunk(60).join("\n") %] Split string up into a list of chunks of text 60 chars wide.
- collapse
-
[% item.collapse %] Strip leading and trailing whitespace and collapse all other space to one space.
- cos
-
[% item.cos %] Returns the cosine of the item.
Not available in TT - available in HTML::Template::Expr.
- defined
-
[% item.defined %] Always true - because the undef sub translates all undefs to ''.
- eval
-
[% item.eval %]
Process the string as though it was a template. This will
start the parsing engine and will use the same configuration as the
current process. Alloy is several times faster at doing this than TT is
and is considered acceptable.
This is a filter and is not available via the Text virtual
object.
Template::Alloy has attempted to make the compile process
painless and fast. By default an MD5 sum of evaled is taken and used to
cache the AST. This behavior can be disabled using the CACHE_STR_REFS
configuration item.
Template::Alloy also allows for named parameters to be passed
to the eval filter.
[% '[% 1 + 2 %]'.eval %]
[% '${ 1 + 2 }'.eval(interpolate => 1) %]
[% "#get( 1 + 2)"|eval(syntax => 'velocity') %]
[% '<TMPL_VAR EXPR="1 + 2">'.eval(syntax => 'hte') %]
[% '<TMPL_VAR EXPR="1 + 2">'.eval(syntax => 'hte') %]
- evaltt
-
Same as the eval filter.
- exp
-
[% 1.exp %] Something like 2.71828182845905
Returns "e" to the power of the item.
- file
-
Same as the redirect filter.
- fmt
-
[% item.fmt('%d') %]
[% item.fmt('%6s') %]
[% item.fmt('%*s', 6) %]
Similar to format. Returns a string formatted with the passed
pattern. Default pattern is %s. Opposite from of
the sprintf vmethod.
- format
-
[% item.format('%d') %]
[% item.format('%6s') %]
[% item.format('%*s', 6) %]
Print the string out in the specified format. It is similar to
the "fmt" virtual method, except that the item is split on
newline and each line is processed separately.
- hash
-
[% item.hash %] Returns a one item hash with a key of "value" and a value of the item.
- hex
-
[% "FF".hex %]
Returns the decimal value of the passed hex numbers. Note that
you may also just use [% 0xFF %].
Not available in TT - available in HTML::Template::Expr.
- html
-
[% item.html %] Performs a very basic html encoding (swaps out &, <, > and " with the corresponding html entities)
Previously it also encoded the ' but this behavior did not match TT2's behavior. Use .xml to obtain that behavior.
- indent
-
[% item.indent(3) %] Indent by that number of spaces if an integer is passed (default is 4).
[% item.indent("Foo: ") %] Add the string "Foo: " to the beginning of every line.
- int
-
[% item.int %] Return the integer portion of the value (0 if none).
- json
-
[% item.json %] Returns a JSON encoded representation.
[% item.json(1) %] Returns a pretty JSON encoded representation.
- lc
- Same as the lower vmethod. Returns the lowercased version of the
item.
- lcfirst
-
[% item.lcfirst %] Lowercase the leading letter.
- length
-
[% item.length %] Return the length of the string.
- list
-
[% item.list %] Returns a list (arrayref) with a single value of the item.
- log
-
[% 8.exp.log %] Equal to 8.
Returns the natural log base "e" of the item.
Not available in TT - available in HTML::Template::Expr.
- lower
-
[% item.lower %] Return the string lowercased.
- match
-
[% item.match("(\w+) (\w+)") %] Return a list of items matching the pattern.
[% item.match("(\w+) (\w+)", 1) %] Same as before - but match globally.
In Template::Alloy and TT3 you can use regular expressions
notation as well.
[% item.match( /(\w+) (\w+)/ ) %] Same as before.
[% item.match( m{(\w+) (\w+)} ) %] Same as before.
Note that you can't use the 'g' regex modifier - you must pass
the second argument to turn on global match.
- none
- Returns the item without modification. This was added as a compliment case
when the AUTO_FILTER configuration is specified. Note that it must be
called as a filter to bypass the application of the AUTO_FILTER.
[% item | none %] Returns the item without modification.
- null
-
[% item.null %] Return nothing.
If the item contains a coderef it will still be executed, but
the result would be ignored.
- oct
-
[% "377".oct %]
Returns the decimal value of the octal string. On recent
versions of perl you may also pass numbers starting with 0x which will
be interpreted as hexadecimal, and starting with 0b which will be
interpreted as binary.
Not available in TT - available in HTML::Template::Expr.
- rand
-
[% item = 10; item.rand %] Returns a number greater or equal to 0 but less than 10.
[% 1.rand %]
Note: This filter is not available as of TT2.15.
- remove
-
[% item.remove("\s+") %] Same as replace - but is global and replaces with nothing.
- redirect
-
[% item.redirect("output_file.html") %]
Writes the contents out to the specified file. The filename
must be relative to the OUTPUT_PATH configuration variable and the
OUTPUT_PATH variable must be set.
This is a filter and is not available via the Text virtual
object.
- repeat
-
[% item.repeat(3) %] Repeat the item 3 times
[% item.repeat(3, ' | ') %] Repeat the item 3 times separated with ' | '
- replace
-
[% item.replace("\s+", " ") %] Globally replace all space with
[% item.replace("foo", "bar", 0) %] Replace only the first instance of foo with bar.
[% item.replace("(\w+)", "($1)") %] Surround all words with parenthesis.
In Template::Alloy and TT3 you may also use normal regular
expression notation.
[% item.replace(/(\w+)/, "($1)") %] Same as before.
Note that you can't use the 'g' regex modifier - global match
is on by default. You must pass the third argument of false to turn off
global match.
- return
- Returns the item from the inner most block, macro, or file. Similar to the
RETURN directive.
[% item.return %]
[% RETURN item %]
- search
-
[% item.search("(\w+)") %] Tests if the given pattern is in the string.
In Template::Alloy and TT3 you may also use normal regular
expression notation.
[% item.search(/(\w+)/) %] Same as before.
- sin
-
[% item.sin %] Returns the sine of the item.
- size
-
[% item.size %] Always returns 1.
- split
-
[% item.split %] Returns an arrayref from the item split on " "
[% item.split("\s+") %] Returns an arrayref from the item split on /\s+/
[% item.split("\s+", 3) %] Returns an arrayref from the item split on /\s+/ splitting until 3 elements are found.
In Template::Alloy and TT3 you may also use normal regular
expression notation.
[% item.split( /\s+/, 3 ) %] Same as before.
- sprintf
-
[% item = "%d %d" %]
[% item.sprintf(7, 8) %]
Uses the pattern stored in self, and passes it to sprintf with
the passed arguments. Opposite from the fmt vmethod.
- sqrt
-
[% item.sqrt %]
Returns the square root of the number.
- srand
- Calls the perl srand function to set the internal random seed. This will
affect future calls to the rand vmethod.
- stderr
-
[% item.stderr %] Print the item to the current STDERR handle.
- substr
-
[% item.substr(i) %] Returns a substring of item starting at i and going to the end of the string.
[% item.substr(i, n) %] Returns a substring of item starting at i and going n characters.
- trim
-
[% item.trim %] Strips leading and trailing whitespace.
- uc
- Same as the upper command. Returns uppercased string.
- ucfirst
-
[% item.ucfirst %] Uppercase the leading letter.
- upper
-
[% item.upper %] Return the string uppercased.
- uri
-
[% item.uri %] Perform a very basic URI encoding.
- url
-
[% item.url %] Perform a URI encoding - but some characters such
as : and / are left intact.
- xml
-
[% item.xml %] Performs a very basic xml encoding (swaps out &, <, >, ' and " with the corresponding xml entities)