The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Text::Xslate::Manual::Builtin - Builtin methods and filters/functions in Xslate

DESCRIPTION

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.

METHODS

The xslate engine supports auto-boxing, so you can call methods for primitive (non-object) values. The following is builtin methods.

For nil

nil has its specific namespace as nil.

$s.defined()

Returns false.

For SCALARs

The namespace of SCALARs is scalar.

$s.defined()

Returns true.

For ARRAY references

The namespace of ARRAY references is array.

$a.defined()

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.

Examples:

    : 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.

For HASH references

The namespace of HASH references is hash.

$h.defined()

Returns true.

$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.

For example:

    : for $hash_ref.kv() -> $pair {
        <: $pair.key :>=<: $pair.value :>
    : }

FILTERS/FUNCTIONS

The xslate engine supports filter syntax as well as function call. The following is the builtin functions, which can be invoked as filter syntax. =head2 mark_raw($str)

Mark $str as a raw string to avoid auto HTML escaping.

raw is an alias to 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($str)

Escapes html meta characters in str. If str is a raw string, this function returns str as is.

The html meta characters are /[<"'&]/>.

uri($str)

Escapes unsafe URI characters in $str.

The unsafe URI characters are characters not included in the unreserved character class defined by RFC 3986, i.e. /[^A-Za-z0-9\-\._~]/.

See also RFC 3986.

dump($value)

Inspects $value with Data::Dumper.

This function is provided for testing and debugging.

SEE ALSO

Text::Xslate

Text::Xslate::Manual