Marquee Marquee::SSIHandler::EP - EP template handler

TABLE OF CONTENTS

NAME

Marquee::SSIHandler::EP - EP template handler

SYNOPSIS

my $ep = Marquee::SSIHandler::EP->new;
$ep->render('/path/to/template.html.ep');

DESCRIPTION

Marquee::SSIHandler::EP is a EP handler.

ATTRIBUTES

Marquee::SSIHandler::EP inherits all attributes from Marquee::SSIHandler::EPL and implements the following new ones.

funcs

A Hash ref that contains template functions.

$ep->funcs->{some_func} = sub {...};

You can use "add_function" method to add a function entry instead of the code above.

FUNCTIONS

Following template functions are automatically available.

b

shortcut for Mojo::ByteStream constructor. Mojo::ByteStream objects are always excluded from automatic escaping.

<%= b('<div></div>') %>

current_template

Returns current template path.

<% my $path = current_template(); %>

docwrite

Append document content immidiately within any template syntax. The return value is always XML escaped unless when the input is Mojo::ByteStream object.

<%
    if ($yes) {
        docwrite('yes');
    } else {
        docwrite('nop');
    }
    docwrite(' is the answer!');
%>

The above is paraphrase of below.

% if ($yes) {
    yes
% } else {
    nop
% }
is the answer!

extends

"extends" function cooperates with "placeholder" and "override", provides template inheritance mechanism.

Base template named layout/common.html.ep.

<!doctype html>
<html>
    <head>
        <title><%= placeholder 'title' => begin %>DEFAULT TITLE<% end %></title>
    </head>
    <body>
        <div id="main">
            <%= placeholder 'main' => begin %>
                DEFAULT MAIN
            <% end %>
        </div>
        <div id="main2">
            <%= placeholder 'main2' => begin %>
                DEFAULT MAIN2
            <% end %>
        </div>
    </body>
</html>

A template can extends common.html.ep as follows. The path can be relative to current template directory or relative to document root if leading slashed. The handler is auto detected so you don't need to specify the extension.

<%= extends './layout/common.html' => begin %>
    <% override 'title' => begin %>
        title
    <% end %>
    <% override 'main' => begin %>
        <div>
            main content<%= time %>
        </div>
    <% end %>
<% end %>

extends_as

"extends_as" inherits a template and extends it. This function is similar to "extends" but you can specify the handler the template would be parsed with.

Note that the template.html MUST NOT be name as template.html.ep

<%= extends_as './path/to/template.html', 'ep' => begin %>
...
<% end %>

iter

Array iterator with a block.

<%= iter @array => begin %>
    <% my ($elem, $index) = @_; %>
    No.<%= $index %> is <%= $elem %>
<% end %>

Array refs and Hash refs are also accepted.

<%= iter $array_ref => begin %>
    <% my ($elem, $index) = @_; %>
    No.<%= $index %> is <%= $elem %>
<% end %>

<%= iter $hash_ref => begin %>
    <% my ($key, $value) = @_; %>
    <%= $key %> is <%= $value %>
<% end %>

include

Include a template or a static files into current template. The path can be relative to current template directory or relative to document root if leading slashed.

<%= include('./path/to/template.html', key => value) %>
<%= include('/path/to/template.html', key => value) %>

include_as

Include a template into current template. This function is similar to include but you can specify the handler the template would be parsed with.

<%= include_as('./path/to/template.html', 'ep', key => value) %>

override

Override placeholder. See "extends" method.

param

Returns request parameters for given key.

<%= param('key') %>

placeholder

Set placeholder with default block. See "extends" method.

session

Sets or get a session value or get all data in hash.

<%= session('key') %>
<% session('key', 'value'); %>
<% $hash = session(); %>

stash

Returns stash value for given key.

<%= stash('key') %>

to_abs

Generates absolute path for server filesystem root with given relative one. Leading dot-segment indicates current file and the leading slash indicates Marquee root.

<%= to_abs('/path.css') %> <!-- /path/to/Marquee/path.css -->

At /path/to/Marquee/html/category/index.html

<%= to_abs('./path.css') %> <!-- /path/to/Marquee/html/category/path.css  -->

url_for

Generates a portable URL relative to document root.

<%= url_for('./b.css') %> # current is '/a/.html' then generates '/a/b.css'
<%= url_for('/b.css') %>  # current is '/a/.html' then generates '/b.css'

CLASS METHODS

Marquee::SSIHandler::EP inherits all class methods from Marquee::SSIHandler::EPL and implements the following new ones.

new

Constructor.

my $ep = Marquee::SSIHandler::EP->new;

INSTANCE METHODS

Marquee::SSIHandler::EP inherits all instance methods from Marquee::SSIHandler::EPL and implements the following new ones.

add_function

Adds a function to the renderer.

$ep->add_function(html_to_text => sub {
    my ($ep, $html) = @_;
    return Mojo::DOM->new($html)->all_text;
});

in templates...

<%= html_to_text($html) %>

render

Renders given template and returns the result. If rendering fails, die with Mojo::Exception.

$ep->render('/path/to/template.html.ep');

url_for

Generates a portable URL relative to document root.

$ep->url_for('./path.css')

SEE ALSO

Marquee::SSIHandler, Marquee, Mojolicious