.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Text::Xslate::Manual::Builtin 3pm" .TH Text::Xslate::Manual::Builtin 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Text::Xslate::Manual::Builtin \- Builtin methods and filters/functions in Xslate .SH DESCRIPTION .IX Header "DESCRIPTION" This document describes builtin methods and filters/functions in Xslate. .PP Note that the xslate engine is not aware of \fBcontext\fR, so all the methods and filters/functions return a single value, even when the equivalent of Perl's returns a list of values. .PP Note that optional functions are defined in Text::Xslate::Bridge::Star. .SH METHODS .IX Header "METHODS" The xslate engine supports auto-boxing, so you can call methods for primitive (non-object) values. The following are builtin methods. .SS "For nil" .IX Subsection "For nil" \&\f(CW\*(C`nil\*(C'\fR has its specific namespace as \f(CW\*(C`nil\*(C'\fR, although no builtin methods are provided. .SS "For SCALARs" .IX Subsection "For SCALARs" The namespace of SCALARs is \f(CW\*(C`scalar\*(C'\fR, although no builtin methods are provided. .SS "For ARRAY references" .IX Subsection "For ARRAY references" The namespace of ARRAY references is \f(CW\*(C`array\*(C'\fR. .PP \fR\f(CI\*(C`$arrayref.first()\*(C'\fR\fI\fR .IX Subsection "$arrayref.first()" .PP Returns the first element of \fR\f(CI$arrayref\fR\fI\fR. .PP \fR\f(CI\*(C`$arrayref.last()\*(C'\fR\fI\fR .IX Subsection "$arrayref.last()" .PP Returns the last element of \fR\f(CI$arrayref\fR\fI\fR. .PP \fR\f(CI\*(C`$arrayref.size()\*(C'\fR\fI\fR .IX Subsection "$arrayref.size()" .PP Returns the number of elements in \fR\f(CI$arrayref\fR\fI\fR. .PP \fR\f(CI\*(C`$arrayref.join($separator)\*(C'\fR\fI\fR .IX Subsection "$arrayref.join($separator)" .PP Joins the elements of \fR\f(CI$arrayref\fR\fI\fR into a single string separated by \&\fI\fR\f(CI$separator\fR\fI\fR. .PP \fR\f(CI\*(C`$arrayref.reverse()\*(C'\fR\fI\fR .IX Subsection "$arrayref.reverse()" .PP Returns an ARRAY reference consisting of the elements of \fR\f(CI$arrayref\fR\fI\fR in the opposite order. .PP \fR\f(CI\*(C`$arrayref.sort(?$callback)\*(C'\fR\fI\fR .IX Subsection "$arrayref.sort(?$callback)" .PP Sorts \fR\f(CI$arrayref\fR\fI\fR and returns a new ARRAY reference. The optional \fI\fR\f(CI$callback\fR\fI\fR is the same as Perl's. .PP Examples: .PP .Vb 7 \& : my $arrayref = [2, 1, 10]; \& : # alphabetic sort (default) \& : $arrayref.sort().join(" "); # 1 10 2 \& : # explicitly alphabetic \& : $arrayref.sort(\-> $a, $b { $a cmp $b }).join(" "); # 1 10 2 \& : # numeric sort \& : $arrayref.sort(\-> $a, $b { $a <=> $b }).join(" "); # 1 2 10 .Ve .PP See also "sort" in perlfunc. .PP \fR\f(CI\*(C`$arrayref.map($callback)\*(C'\fR\fI\fR .IX Subsection "$arrayref.map($callback)" .PP Evaluates \fR\f(CI$callback\fR\fI\fR for each element of \fI\fR\f(CI$arrayref\fR\fI\fR and returns a new ARRAY reference composed of the result of each such evaluation. .PP Examples: .PP .Vb 6 \& : my $arrayref = [1, 2, 4, 8, 16]; \& : # double \& : $arrayref.map(\-> $a { $a * 2 }).join(\*(Aq,\*(Aq); # 2,4,8,16,32 \& : # sequence \& : my $hashref = {a => 1, b => 2, c => 3, d => 4}; \& : [\*(Aqb\*(Aq, \*(Aqd\*(Aq, \*(Aqa\*(Aq].map(\-> $a {$hashref[$a]}).join(\*(Aq,\*(Aq); # 2,4,1 .Ve .PP See also "map" in perlfunc .PP \fR\f(CI\*(C`$arrayref.reduce($callback)\*(C'\fR\fI\fR .IX Subsection "$arrayref.reduce($callback)" .PP Reduces \fR\f(CI$arrayref\fR\fI\fR by calling \fI\fR\f(CI$callback\fR\fI\fR multiple times. If \fI\fR\f(CI$arrayref\fR\fI\fR is empty, this method returns \f(CW\*(C`nil\*(C'\fR. .PP Examples: .PP .Vb 9 \& : my $arrayref = [10, 20, 30]; \& : # sum \& : $arrayref.reduce(\-> $a, $b { $a + $b }); # 60 \& : # concat \& : $arrayref.reduce(\-> $a, $b { $a ~ $b }); # 102030 \& : # min \& : $arrayref.reduce(\-> $a, $b { $a min $b }); # 10 \& : # max \& : $arrayref.reduce(\-> $a, $b { $a max $b }); # 30 .Ve .PP See also "reduce" in List::Util. .PP \fR\f(CI\*(C`$arrayref.merge($v)\*(C'\fR\fI\fR .IX Subsection "$arrayref.merge($v)" .PP Returns a new ARRAY reference consisting of \fR\f(CI$arrayref\fR\fI\fR and \fI\fR\f(CI$v\fR\fI\fR. .PP \&\fR\f(CI$v\fR\fI\fR may be an ARRAY reference or a scalar value. .SS "For HASH references" .IX Subsection "For HASH references" The namespace of HASH references is \f(CW\*(C`hash\*(C'\fR. .PP \fR\f(CI\*(C`$hashref.size()\*(C'\fR\fI\fR .IX Subsection "$hashref.size()" .PP Returns the number of entries of \fR\f(CI$hashref\fR\fI\fR. .PP .Vb 2 \& : my $hashref = {a => 1, b => 2, c => 3, d => 4}; \& : $hashref.size(); # 4 .Ve .PP \fR\f(CI\*(C`$hashref.keys()\*(C'\fR\fI\fR .IX Subsection "$hashref.keys()" .PP Returns an ARRAY reference consisting of the keys of \fR\f(CI$hashref\fR\fI\fR, which are sorted by the keys. .PP .Vb 2 \& : my $hashref = {a => 1, b => 2, c => 3, d => 4}; \& : $hashref.keys().join(\*(Aq \*(Aq); # a b c d .Ve .PP \fR\f(CI\*(C`$hashref.values()\*(C'\fR\fI\fR .IX Subsection "$hashref.values()" .PP Returns an ARRAY reference consisting of the values of \fR\f(CI$hashref\fR\fI\fR, which are sorted by the keys. .PP .Vb 2 \& : my $hashref = {a => 1, b => 2, c => 3, d => 4}; \& : $hashref.values().join(\*(Aq \*(Aq); # 1 2 3 4 .Ve .PP \fR\f(CI\*(C`$hashref.kv()\*(C'\fR\fI\fR .IX Subsection "$hashref.kv()" .PP Returns an ARRAY reference consisting of the key-value pairs of \fR\f(CI$hashref\fR\fI\fR, which are sorted by the keys. Each pair is an object that has the \f(CW\*(C`keys\*(C'\fR and \&\f(CW\*(C`value\*(C'\fR attributes. .PP For example: .PP .Vb 3 \& : for $hashref.kv() \-> $pair { \& <: $pair.key :>=<: $pair.value :> \& : } .Ve .PP Output: .PP .Vb 4 \& a=1 \& b=2 \& c=3 \& d=4 .Ve .PP \fR\f(CI\*(C`$hashref.merge($v)\*(C'\fR\fI\fR .IX Subsection "$hashref.merge($v)" .PP Returns a new HASH reference consisting of \fR\f(CI$hashref\fR\fI\fR and \fI\fR\f(CI$v\fR\fI\fR. .PP .Vb 3 \& : my $hashref = {a => 1, b => 2, c => 3, d => 4}; \& : my $new = $hashref.merge({a => 0, e => 5}); \& : # {a => 0, b => 2, c => 3, d => 4, e => 5} .Ve .PP \&\fR\f(CI$v\fR\fI\fR must be a HASH reference. .SH "LOOP VARIABLES" .IX Header "LOOP VARIABLES" You can use special loop variables in \f(CW\*(C`for\*(C'\fR loops, although its forms vary in template syntaxes, i.e. \f(CW\*(C`$~item\*(C'\fR in Kolon and \f(CW\*(C`loop\*(C'\fR in TTerse. In this list, the name of the loop variable is represented as \f(CW\*(C`$~item\*(C'\fR. .PP See also "Loops" in Text::Xslate::Syntax::Kolon and "Loops" in Text::Xslate::Syntax::TTerse. .ie n .SS """$~item / $~item.index""" .el .SS "\f(CW$~item / $~item.index\fP" .IX Subsection "$~item / $~item.index" The current iterating index in the loop, which starts \fB0\fR. .ie n .SS """$~item.count""" .el .SS \f(CW$~item.count\fP .IX Subsection "$~item.count" The current iterating count in the loop, which starts \fB1\fR. i.e. the same as \f(CW\*(C`$~item + 1\*(C'\fR. .ie n .SS """$~item.cycle(...)""" .el .SS \f(CW$~item.cycle(...)\fP .IX Subsection "$~item.cycle(...)" Selects a value in the arguments in cycle. .PP For example: .PP .Vb 3 \& : for $arrayref \-> $item { \& <: $~item.cycle(\*(Aqodd\*(Aq, \*(Aqeven\*(Aq) :> \& : } .Ve .PP It will print \f(CW\*(C`odd even odd even ...\*(C'\fR. .ie n .SS """$~item.is_first""" .el .SS \f(CW$~item.is_first\fP .IX Subsection "$~item.is_first" True if the loop block is the first, false otherwise. .PP This is aliased to \f(CW\*(C`first\*(C'\fR in TTerse for compatibility with TT2. .ie n .SS """$~item.is_last""" .el .SS \f(CW$~item.is_last\fP .IX Subsection "$~item.is_last" True if the loop block is the last, false otherwise. .PP This is aliased to \f(CW\*(C`last\*(C'\fR in TTerse for compatibility with TT2. .ie n .SS """$~item.peek_next""" .el .SS \f(CW$~item.peek_next\fP .IX Subsection "$~item.peek_next" The next item of the looping array. \f(CW\*(C`nil\*(C'\fR if \f(CW\*(C`is_last\*(C'\fR. i.e. the same as \f(CW\*(C`$~item.is_last ? nil : $~item.body[$~item+1]\*(C'\fR. .ie n .SS """$~item.peek_prev""" .el .SS \f(CW$~item.peek_prev\fP .IX Subsection "$~item.peek_prev" The previous item of the looping array. \f(CW\*(C`nil\*(C'\fR if \f(CW\*(C`is_first\*(C'\fR. i.e. the same as \f(CW\*(C`$~item.is_first ? nil : $~item.body[$~item\-1]\*(C'\fR. .ie n .SS """$~item.body""" .el .SS \f(CW$~item.body\fP .IX Subsection "$~item.body" The reference of the looping array. .ie n .SS """$~item.size""" .el .SS \f(CW$~item.size\fP .IX Subsection "$~item.size" The size of the looping array. i.e. \f(CWscalar(@{$arrayref})\fR in Perl. .ie n .SS """$~item.max_index""" .el .SS \f(CW$~item.max_index\fP .IX Subsection "$~item.max_index" The maximum index of the looping array. i.e. \f(CW$#{$arrayref}\fR in Perl. .SH FILTERS/FUNCTIONS .IX Header "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. .PP For example, the following two statements are the same: .PP .Vb 2 \& <: $value | foo :> \& <: foo($value) :> .Ve .PP Note that some builtin functions, such as \f(CW\*(C`defined\*(C'\fR, are not a real function which you cannot use as a filter. .ie n .SS mark_raw($str) .el .SS \f(CWmark_raw($str)\fP .IX Subsection "mark_raw($str)" Mark \fR\f(CI$str\fR\fI\fR as a raw string to avoid auto HTML escaping. You'd better avoid to use this function. Instead, you should use the \&\f(CWmark_raw()\fR subroutine in programs, which you can import from \&\f(CW\*(C`Text::Xslate::Util\*(C'\fR. .PP \&\f(CW\*(C`raw\*(C'\fR is an alias to \f(CW\*(C`mark_raw\*(C'\fR. .ie n .SS unmark_raw($str) .el .SS \f(CWunmark_raw($str)\fP .IX Subsection "unmark_raw($str)" Remove the raw mark from \fR\f(CI$str\fR\fI\fR. If \fI\fR\f(CI$str\fR\fI\fR is not a raw string, this function returns \fI\fR\f(CI$str\fR\fI\fR as is. .ie n .SS html_escape($str) .el .SS \f(CWhtml_escape($str)\fP .IX Subsection "html_escape($str)" Escapes html meta characters in \fR\f(CI$str\fR\fI\fR. If \fI\fR\f(CI$str\fR\fI\fR is a raw string, this function returns \fI\fR\f(CI$str\fR\fI\fR as is. .PP The html meta characters are \f(CW\*(C`/[<>"\*(Aq&]/\*(C'\fR. .PP \&\f(CW\*(C`html\*(C'\fR is an alias to \f(CW\*(C`html_escape\*(C'\fR. .ie n .SS uri_escape($str) .el .SS \f(CWuri_escape($str)\fP .IX Subsection "uri_escape($str)" Escapes unsafe URI characters in \fR\f(CI$str\fR\fI\fR which gets encoded to UTF\-8. .PP The unsafe URI characters are characters not included in the \f(CW\*(C`unreserved\*(C'\fR character class defined by RFC 3986, i.e. \f(CW\*(C`/[^A\-Za\-z0\-9\e\-\e._~]/\*(C'\fR. .PP \&\f(CW\*(C`uri\*(C'\fR is an alias to \f(CW\*(C`uri_escape\*(C'\fR. .ie n .SS is_array_ref(($value) .el .SS \f(CWis_array_ref(($value)\fP .IX Subsection "is_array_ref(($value)" Returns true if \fR\f(CI$value\fR\fI\fR is an ARRAY reference. .ie n .SS is_hash_ref(($value) .el .SS \f(CWis_hash_ref(($value)\fP .IX Subsection "is_hash_ref(($value)" Returns true if \fR\f(CI$value\fR\fI\fR is a HASH reference. .ie n .SS dump($value) .el .SS \f(CWdump($value)\fP .IX Subsection "dump($value)" Inspects \fR\f(CI$value\fR\fI\fR with \f(CW\*(C`Data::Dumper\*(C'\fR. .PP This function is provided for testing and debugging. .ie n .SS defined($value) .el .SS \f(CWdefined($value)\fP .IX Subsection "defined($value)" Returns true if \fR\f(CI$value\fR\fI\fR is defined. This is not a real function, but an unary operator, so you can omit the parens like \f(CW\*(C`defined $value\*(C'\fR. .SH "SEE ALSO" .IX Header "SEE ALSO" Text::Xslate .PP Text::Xslate::Manual .PP Text::Xslate::Bridge::Star