Skip to content

Commit 945c79b

Browse files
author
Stefan Geneshky
committed
Create module
1 parent b3c4edb commit 945c79b

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

dist.ini

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name = Kelp-Module-Template-Toolkit
2+
abstract = Template rendering for Kelp via Template
3+
4+
[@Basic]
5+
[AutoPrereqs]
6+
[VersionFromModule]
7+
8+
[PodSyntaxTests]
9+
10+
[GithubMeta]
11+
[Git::ExcludeUntracked]

lib/Kelp/Module/Template/Toolkit.pm

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package Kelp::Module::Template::Toolkit;
2+
use Kelp::Base 'Kelp::Module::Template';
3+
use Template;
4+
5+
our $VERSION = 0.01;
6+
7+
attr ext => 'tt';
8+
9+
sub build_engine {
10+
my ( $self, %args ) = @_;
11+
return Template->new( \%args );
12+
}
13+
14+
sub render {
15+
my ( $self, $template, $vars, @rest ) = @_;
16+
my $output;
17+
$self->engine->process( $template, $vars, \$output, @rest );
18+
return $output;
19+
}
20+
21+
1;
22+
23+
__END__
24+
25+
=pod
26+
27+
=head1 NAME
28+
29+
Kelp::Module::Template::Toolkit - Template::Toolkit processing for Kelp applications
30+
31+
=head1 SYNOPSIS
32+
33+
First ...
34+
35+
# conf/config.pl
36+
{
37+
modules => ['Template::Toolkit'],
38+
modules_init => {
39+
'Template::Toolkit' => { ... }
40+
}
41+
};
42+
43+
Then ...
44+
45+
# lib/MyApp.pm
46+
sub some_route {
47+
my $self = shift;
48+
$self->template('some_template', { bar => 'foo' });
49+
}
50+
51+
=head1 DESCRIPTION
52+
53+
This module provides an interface for using L<Template> inside a Kelp web application.
54+
55+
=head1 REGISTERED METHODS
56+
57+
=head2 template
58+
59+
C<template($filename, \%vars)>
60+
61+
Renders a file using the currently loaded template engine.
62+
63+
=head1 PERKS
64+
65+
=head2 UTF8
66+
67+
L<Template> is sometimes unable to detect the correct encoding, so to ensure
68+
proper rendering, you may want to add C<ENCODING> to its configuration:
69+
70+
# conf/config.pl
71+
{
72+
modules => ['Template::Toolkit'],
73+
modules_init => {
74+
'Template::Toolkit' => {
75+
ENCODING => 'utf8'
76+
}
77+
}
78+
};
79+
80+
81+
=back
82+
83+
=cut

t/config.pl

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
modules => ['Template::Toolkit'],
3+
modules_init => {
4+
'Template::Toolkit' => {
5+
ENCODING => 'utf8'
6+
}
7+
}
8+
};

t/main.t

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use Kelp::Base -strict;
2+
use Test::More;
3+
use Kelp;
4+
5+
my $app = Kelp->new;
6+
my $tmpl = '[% IF !bar %]foo[% ELSE %]bar[% END %]';
7+
8+
can_ok $app, $_ for qw/template/;
9+
like $app->template( \$tmpl, { bar => 1 } ), qr/bar/;
10+
unlike $app->template( \$tmpl, { bar => 1 } ), qr/foo/;
11+
like $app->template( \$tmpl, { bar => 0 } ), qr/foo/;
12+
unlike $app->template( \$tmpl, { bar => 0 } ), qr/bar/;
13+
14+
done_testing;

0 commit comments

Comments
 (0)