Petal::Utils(3pm) | User Contributed Perl Documentation | Petal::Utils(3pm) |
Petal::Utils - Useful template modifiers for Petal.
# install the default set of Petal modifiers: use Petal::Utils; # you can also install modifiers manually: Petal::Utils->install( 'some_modifier', ':some_set' ); # see below for modifiers available & template syntax
The Petal::Utils package contains commonly used Petal modifiers (or plugins), and bundles them with an easy-to-use installation interface. By default, a set of modifiers are installed into Petal when you use this module. You can change which modifiers are installed by naming them after the use statement:
# use the default set: use Petal::Utils qw( :default ); # use the date set of modifiers: use Petal::Utils qw( :date ); # use only named modifiers, plus the debug set: use Petal::Utils qw( UpperCase Date :debug ); # don't install any modifiers use Petal::Utils qw();
You'll find a list of plugin sets throughout this document. You can also get a complete list by looking at the variable:
%Petal::Utils::PLUGIN_SET;
For details on how the plugins are installed, see the "Advanced Petal" section of the Petal documentation.
Each modifier is listed under the set it belongs to.
<p tal:content="lc: $string">lower</p>
<p tal:content="uc: $string">upper</p>
<p tal:content="uc_first: $string">uc_first</p>
<span petal:content="substr:$str">string</span> # does nothing <span petal:content="substr:$str 2">string</span> # cuts the first two chars <span petal:content="substr:$str 2 5">string</span> # extracts chars 2-7 <span petal:content="substr:$str 2 5 1">string with ellipsis</span> # same as above and adds an ellipsis
<p petal:content="printf:'%s' 'Astro'">Astro</p> <p petal:content="printf:'$%0.2f' '2.5'">$2.50</p>
<span tal:replace="date: $date">Jan 1 1970 01:00:01</span>
<p tal:content="us_date: $date">2003-09-05</p>
<p tal:attributes="class if: on_a_page then: a_class else: another_class"> Some text here... </p>
<p tal:if="or: $first $second"> first or second = <span tal:replace="or: $first $second">or</span> </p>
first and second = <span tal:replace="and: $first $second">and</span>
first eq second = <span tal:replace="eq: $first $second">equal</span>
name like regex = <span tal:replace="like: $name ^Will.+m">like</span>
'expression' is the value to compare. 'search' is the value that is compared against expression. 'result' is the value returned, if expression is equal to search. 'default. is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found). <p petal:content="decode:$str 'dog' 'Satchel'">100</p> # if $str = dog, returns Satchel <p petal:content="decode:$str 'cat' 'Buckey' 'Satchel'">Astro</p> # if $str = cat, returns Buckey, else Satchel
<ul> <li tal:repeat="item sort: $array_ref">$item</li> </ul>
<ul> <li tal:repeat="item limit: $array_ref 2">$item</li> </ul>
<ul> <li tal:repeat="item limitr: $array_ref 2">$item</li> </ul>
<ul> <li tal:repeat="key keys: $hash_ref"><span tal:replace="key">key</span></li> </ul>
<ul> <li tal:repeat="item each: $hash_ref"> <span tal:replace="item/key">key</span> => <span tal:replace="item/val">value</span> </li> </ul>
<a href="http://foo/get.html?item=${uri_escape: item/key}">get $item/key</a>
<a petal:attr="href create_href:$url">HTTP Link</a> <a petal:attr="href create_href:$url ftp">FTP Link</a>
dump name: <span tal:replace="dump: name">dump</span>
At the time of writing, the following supersets were available:
':none' => [], ':all' => [qw( :default :hash :debug )], ':default' => [qw( :text :date :logic :list )],
See %Petal::Utils::PLUGIN_SET for an up-to-date list.
Contributions to the modifiers are welcome! You can suggest new modifiers to add to the suite. You will have better luck getting your modifier added by providing a module (see lib/Petal/Utils/And.pm for an example), a patch to Utils.pm (with a modified PLUGIN_SET and documentation for your new modifier), and a test suite. All modifiers are subject to the discretion of the authors.
William McKee <william@knowmad.com>, and Steve Purkis <spurkis@cpan.org>
Copyright (c) 2003-2004 William McKee & Steve Purkis.
This module is free software and is distributed under the same license as Perl itself. Use it at your own risk.
Thanks to Jean-Michel Hiver for making Petal available to the Perl community.
Petal
2022-12-12 | perl v5.36.0 |