Venus::Template - Template Class
Template Class for Perl 5
package main; use Venus::Template; my $template = Venus::Template->new( 'From: <{{ email }}>', ); # $template->render; # "From: <>"
This package provides a templating system, and methods for rendering templates using simple markup and minimal control structures. The default opening and closing markers, denoting a template token, block, or control structure, are {{ and }}. A token takes the form of {{ foo }} or {{ foo.bar }}. A block takes the form of {{ for foo.bar }} where foo.bar represents any valid path, resolvable by "path" in Venus::Array or "path" in Venus::Hash, which returns an arrayref or Venus::Array object, and must be followed by {{ end foo }}. Control structures take the form of {{ if foo }} or {{ if not foo }}, may contain a nested {{ else foo }} control structure, and must be followed by {{ end foo }}. Leading and trailing whitespace is automatically removed from all replacements.
{{
}}
{{ foo }}
{{ foo.bar }}
{{ for foo.bar }}
foo.bar
{{ end foo }}
{{ if foo }}
{{ if not foo }}
{{ else foo }}
This package has the following attributes:
variables(HashRef)
This attribute is read-write, accepts (HashRef) values, is optional, and defaults to {}.
(HashRef)
{}
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Accessible
Venus::Role::Buildable
Venus::Role::Explainable
Venus::Role::Valuable
This package provides the following methods:
The render method processes the template by replacing the tokens and control structurs with the appropriate replacements and returns the result.
# given: synopsis; my $result = $template->render; # "From: <>"
# given: synopsis; $template->value( 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>', ); $template->variables({ email => 'noreply@example.com', }); my $result = $template->render; # "From: <noreply@example.com>"
# given: synopsis; $template->value( 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>', ); $template->variables({ name => 'No-Reply', email => 'noreply@example.com', }); my $result = $template->render; # "From: No-Reply <noreply@example.com>"
package main; use Venus::Template; my $template = Venus::Template->new(q( {{ for chat.messages }} {{ user.name }}: {{ message }} {{ end chat.messages }} )); $template->variables({ chat => { messages => [ { user => { name => 'user1' }, message => 'ready?' }, { user => { name => 'user2' }, message => 'ready!' }, { user => { name => 'user1' }, message => 'lets begin!' }, ]} }); my $result = $template->render; # user1: ready? # user2: ready! # user1: lets begin!
package main; use Venus::Template; my $template = Venus::Template->new(q( {{ for chat.messages }} {{ if user.legal }} {{ user.name }} [18+]: {{ message }} {{ else user.legal }} {{ user.name }} [-18]: {{ message }} {{ end user.legal }} {{ end chat.messages }} )); $template->variables({ chat => { messages => [ { user => { name => 'user1', legal => 1 }, message => 'ready?' }, { user => { name => 'user2', legal => 0 }, message => 'ready!' }, { user => { name => 'user1', legal => 1 }, message => 'lets begin!' }, ]} }); my $result = $template->render; # user1 [18+]: ready? # user2 [-18]: ready! # user1 [18+]: lets begin!
package main; use Venus::Template; my $template = Venus::Template->new(q( {{ for chat.messages }} {{ if user.admin }}@{{ end user.admin }}{{ user.name }}: {{ message }} {{ end chat.messages }} )); $template->variables({ chat => { messages => [ { user => { name => 'user1', admin => 1 }, message => 'ready?' }, { user => { name => 'user2', admin => 0 }, message => 'ready!' }, { user => { name => 'user1', admin => 1 }, message => 'lets begin!' }, ]} }); my $result = $template->render; # @user1: ready? # user2: ready! # @user1: lets begin!
This package overloads the following operators:
("")
This package overloads the "" operator.
""
example 1
# given: synopsis; my $result = "$template"; # "From: <>"
example 2
# given: synopsis; my $result = "$template, $template"; # "From: <>, From: <>"
(~~)
This package overloads the ~~ operator.
~~
# given: synopsis; my $result = $template ~~ 'From: <>'; # 1
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.