NAME

Venus::Template - Template Class

ABSTRACT

Template Class for Perl 5

SYNOPSIS

package main;

use Venus::Template;

my $template = Venus::Template->new(
  'From: <{{ email }}>',
);

# $template->render;

# "From: <>"

DESCRIPTION

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.

ATTRIBUTES

This package has the following attributes:

variables

variables(HashRef)

This attribute is read-write, accepts (HashRef) values, is optional, and defaults to {}.

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Accessible

Venus::Role::Buildable

Venus::Role::Explainable

Venus::Role::Valuable

METHODS

This package provides the following methods:

render

render(Str $template, HashRef $variables) (Str)

The render method processes the template by replacing the tokens and control structurs with the appropriate replacements and returns the result. Note: The rendering process expects variables to be hashrefs and sets (arrayrefs) of hashrefs.

Since 0.01

render example 1
# given: synopsis;

my $result = $template->render;

# "From: <>"
render example 2
# 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>"
render example 3
# 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>"
render example 4
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!
render example 5
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!
render example 6
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!
render example 7
package main;

use Venus::Template;

my $template = Venus::Template->new(q(
  {{ for chat.messages }}
  [{{ loop.place }}] {{ 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;

# [1] user1: ready?
# [2] user2: ready!
# [3] user1: lets begin!
render example 8
package main;

use Venus::Template;

my $template = Venus::Template->new(q(
  {{ for chat.messages }}
  [{{ loop.index }}] {{ 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;

# [0] user1: ready?
# [1] user2: ready!
# [2] user1: lets begin!

OPERATORS

This package overloads the following operators:

operation: ("")

This package overloads the "" operator.

example 1

# given: synopsis;

my $result = "$template";

# "From: <>"

example 2

# given: synopsis;

my $result = "$template, $template";

# "From: <>, From: <>"
operation: (~~)

This package overloads the ~~ operator.

example 1

# given: synopsis;

my $result = $template ~~ 'From: <>';

# 1

AUTHORS

Awncorp, awncorp@cpan.org

LICENSE

Copyright (C) 2000, Al Newkirk.

This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.