Text::Xslate::Manual::Builtin - Builtin methods and filters/functions in Xslate
This document describes builtin methods and filters/functions in Xslate.
Note that the xslate engine is not aware of context, so all the methods and filters/functions return a single value, even when the equivalent of Perl's returns a list of values.
The xslate engine supports auto-boxing, so you can call methods for primitive (non-object) values. The following is builtin methods.
nil has its specific namespace as nil, although no builtin methods are provided.
nil
The namespace of SCALARs is scalar, although no builtin methods are provided.
scalar
The namespace of ARRAY references is array.
array
Returns true;
$a.size()
Returns the number of elements of $a.
$a.join($separator)
Joins the elements of $a into a single string separated by $separator.
$a.reverse()
Returns an ARRAY reference consisting of the elements of $a in the opposite order.
$a.sort(?$callback)
Sorts $a and returns a new ARRAY reference. The optional $callback is the same as Perl's.
Examples:
: my $a = [2, 1, 10]; : # alphabetic sort (default) : $a.sort().join(" "); # 1 10, 2 : # explicitly alphabetic : $a.sort(-> $a, $b { $a cmp $b }).join(" "); # 1, 10, 2 : # numeric sort : $a.sort(-> $a, $b { $a <=> $b }).join(" "); # 1, 2, 10
See also "sort" in perlfunc.
$a.map($callback)
Evaluates $callback for each element of $a and returns a new ARRAY reference composed of the result of each such evaluation.
See also "map" in perlfunc
$a.reduce($callback)
Reduces $a by calling $callback multiple times. If $a is empty, this method returns nil.
: my $a = [10, 20, 30]; : # sum : $a.reduce(-> $a, $b { $a + $b }); # 60 : # concat : $a.reduce(-> $a, $b { $a ~ $b }); # 102030 : # min : $a.reduce(-> $a, $b { $a min $b }); # 10 : # max : $a.reduce(-> $a, $b { $a max $b }); # 30
See also "reduce" in List::Util.
The namespace of HASH references is hash.
hash
$h.size()
Returns the number of entries of $h.
$h.keys()
Returns an ARRAY reference consisting of the keys of $h, which are sorted by the keys.
$h.values()
Returns an ARRAY reference consisting of the values of $h, which are sorted by the keys.
$h.kv()
Returns an ARRAY reference consisting of the key-value pairs of $h, which are sorted by the keys. Each pair is an object that has the keys and value attributes.
keys
value
For example:
: for $hash_ref.kv() -> $pair { <: $pair.key :>=<: $pair.value :> : }
You can use special loop variables in for loops, although its forms vary in template syntaxes, i.e. $~item in Kolon and loop in TTerse. In this list, the name of the loop variable is represented as $~item.
for
$~item
loop
See also "Loops" in Text::Xslate::Syntax::Kolon and "Loops" in Text::Xslate::Syntax::TTerse.
$~item / $~item.index
The current iterating index in the loop, which starts 0.
$~item.count
The current iterating count in the loop, which starts 1. i.e. the same as $~item + 1.
$~item + 1
$~item.cycle(...)
Selects a value in the arguments in cycle.
: for $arrayref -> $item { <: $~item.cycle('odd', 'even') :> : }
It will print odd even odd even ....
odd even odd even ...
$~item.is_first
True if the loop block is the first, false otherwise.
This is aliased to first in TTerse for compatibility with TT2.
first
$~item.is_last
True if the loop block is the last, false otherwise.
This is aliased to last in TTerse for compatibility with TT2.
last
$~item.peek_next
The next item of the looping array. nil if is_last. i.e. the same as $~item.is_last ? nil : $~item.body[$~item+1].
is_last
$~item.is_last ? nil : $~item.body[$~item+1]
$~item.peek_prev
The previous item of the looping array. nil if is_first. i.e. the same as $~item.is_first ? nil : $~item.body[$~item-1].
is_first
$~item.is_first ? nil : $~item.body[$~item-1]
$~item.body
The reference of the looping array.
$~item.size
The size of the looping array. i.e. scalar(@{$arrayref}) in Perl.
scalar(@{$arrayref})
$~item.max_index
The maximum index of the looping array. i.e. $#{$arrayref} in Perl.
$#{$arrayref}
The xslate engine supports filter syntax as well as function call. The following is the builtin functions, which can be invoked as filter syntax.
For example, the following two statements are the same:
<: $value | foo :> <: foo($value) :>
Note that some builtin functions, such as defined, are not a real function which you cannot use as a filter.
defined
mark_raw($str)
Mark $str as a raw string to avoid auto HTML escaping. You'd better avoid to use this function. Instead, you should use the mark_raw() subroutine in programs, which you can import from Text::Xslate::Util.
mark_raw()
Text::Xslate::Util
raw is an alias to mark_raw.
raw
mark_raw
unmark_raw($str)
Remove the raw mark from $str. If $str is not a raw string, this function returns $str as is.
html_escape($str)
Escapes html meta characters in $str. If $str is a raw string, this function returns $str as is.
The html meta characters are /[<>"'&]/.
/[<>"'&]/
html is an alias to html_escape.
html
html_escape
uri_escape($str)
Escapes unsafe URI characters in $str which gets encoded to UTF-8.
The unsafe URI characters are characters not included in the unreserved character class defined by RFC 3986, i.e. /[^A-Za-z0-9\-\._~]/.
unreserved
/[^A-Za-z0-9\-\._~]/
uri is an alias to uri_escape.
uri
uri_escape
is_array_ref(($value)
Returns true if $value is an ARRAY reference.
is_hash_ref(($value)
Returns true if $value is a HASH reference.
dump($value)
Inspects $value with Data::Dumper.
Data::Dumper
This function is provided for testing and debugging.
defined($value)
Returns true if $value is defined. This is not a real function, but an unary operator, so you can omit the parens like defined $value.
defined $value
Text::Xslate
Text::Xslate::Manual
To install Text::Xslate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Xslate
CPAN shell
perl -MCPAN -e shell install Text::Xslate
For more information on module installation, please visit the detailed CPAN module installation guide.