Skip to content

Recipes for templates

Duncan Ferguson edited this page Apr 25, 2017 · 3 revisions

Passing variables to the stash

Variables of all kinds can be easily accessed from templates. It's good design to pass the data from your application to the stash and access it from the templating system.

Passing a scalar to the stash

Here is a small code snippet about passing variables to the stash.

#!/usr/bin/env perl
 
use Mojolicious::Lite;
 
get '/' => sub {
    my $self = shift;
    $self->stash(
        name => 'Peter'
    );
} => 'index';
 
app->start;
__DATA__
 
@@ index.html.ep
% layout 'default';
Hello <%= $name %>
 
@@ layouts/default.html.ep
<!doctype html><html>
    <head><title>Scalar exercise</title></head>
    <body><%== content %></body>
</html>

Passing an array to the stash (by reference)

#!/usr/bin/env perl
 
use Mojolicious::Lite;
 
get '/' => sub {
    my $self = shift;
    $self->stash(
        names => ['Peter', 'Georg', 'Fabian']
    );
} => 'index';
 
app->start;
__DATA__
 
@@ index.html.ep
% layout 'default';
<p>Hello <%= $names->[0] %></p>
 
<p>List of array members:
<ul>
% foreach my $person (@$names) {
<li><%= $person %></li>
% }
</ul>
</p>
 
@@ layouts/default.html.ep
<!doctype html><html>
    <head><title>Array exercise</title></head>
    <body><%== content %></body>
</html>

Passing a hashref to the stash

Now to some more advanced stuff. Passing a hashref is a useful thing as this might be the result of a database query from one of the DBI modules. See selectall_hashref for more information on how to do it.

#!/usr/bin/env perl
 
use Mojolicious::Lite;
 
get '/' => sub {
    my $self = shift;
 
    my $services = {
        'rabenfedern.de'    =>  {
            'admin' => 'rhaen',
            'desc'  => 'webserver'
        },
        'pkgbox.org' => {
            'admin' => 'hazel',
            'desc'  => 'mailserver'
        }
    };
 
    $self->stash(
        services => $services
    );
} => 'index';
 
app->start;
__DATA__
 
@@ index.html.ep
% layout 'default';
<p>Welcome to hashrefs</p>
<p>
<ul>
% foreach my $domain (keys %{$services}) {
   <li><%= $domain %></li>
   <ul>
   <li>Admin: <%= $services->{$domain}->{admin} %></li>
   </ul>
   <p>That's a server for: <%= $services->{$domain}->{desc} %></p>
% }
</ul>
</p>
 
@@ layouts/default.html.ep
<!doctype html><html>
    <head><title>Default</title></head>
    <body><%== content %></body>
</html>

Passing a arrayref to the stash

The following code snippet completes the template code demo session. Voila, working with arrayref inside the stash.

#!/usr/bin/env perl
 
use Mojolicious::Lite;
 
get '/' => sub {
    my $self = shift;
 
    my $ref_pets = [
                        ['isolde', 'cat'],
                        ['oscar',  'dog']
                   ];
 
    $self->stash(
        ref_pets => $ref_pets
    );
} => 'index';
 
app->start;
__DATA__
 
@@ index.html.ep
% layout 'default';
<p>Welcome to arrayrefs</p>
<p>
<ul>
% for my $loop ( @{$ref_pets} ) {
    <li>Name of the pet: <%= $loop->[0] %>, it's a <%= $loop->[1] %></li>
% }
</ul>
</p>
 
@@ layouts/default.html.ep
<!doctype html><html>
    <head><title>Default</title></head>
    <body><%== content %></body>
</html>
Clone this wiki locally