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

METHODS

This package provides the following methods:

render

The render method processes the template by replacing the tokens and control structurs with the appropriate replacements and returns the result.

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!

AUTHORS

Cpanery, cpanery@cpan.org

LICENSE

Copyright (C) 2021, Cpanery

Read the "license" file.