Quispiam

power computing

Let say we have written a cool helper sub and we need to use in lots of templates.

sub apostrophise {
    my $owner = shift;
    if($owner =~ /s$/) {
        return $owner . "'";
    }
    return $owner . "'s";
}

To get this done we need to use the before_template hook and add it to the tokens hash which is passed to the templates

before_template sub {
    my $tokens = shift;
    $tokens->{'apostrophise'} = &apostrophise;
};

Now the helper called apostrophise can be used in oure templates like so

<h1><% apostrophise(client.login) %> files 
<small>The files <% client.login %> has uploaded.</small>


I freakin’ love Dancer

Thanks go to

https://metacpan.org/module/Dancer::Tutorial#Using-before_template

Dancer

The cool people who make Dancer so awesome

Share