1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package Dancer::Template::Xslate;
use strict;
use warnings;
use Carp;
use Dancer::App;
use File::Spec::Functions qw(abs2rel rel2abs);
use Text::Xslate;
use base "Dancer::Template::Abstract";
# VERSION
# ABSTRACT: Text::Xslate wrapper for Dancer
# Note: The standard Xslate template extension is
# "tx" but kept to "tt" for backward compatibility.
sub init {
my ($self) = @_;
my $app = Dancer::App->current;
my %xslate_args = %{$self->config};
## Set default path for header/footer etc.
$xslate_args{path} ||= [];
my $views_dir = $app->setting("views") || "";
push @{ $xslate_args{path} }, $views_dir
if !grep { $_ eq $views_dir } @{ $xslate_args{path} };
## for those who read Text::Xslate instead of Dancer::Template::Abstract
if ( defined $xslate_args{suffix} ) {
$self->config->{extension} = $xslate_args{suffix};
$self->config->{extension} =~ s/^\.//;
}
## Avoid "Text::Xslate: Unknown option(s): extension"
$xslate_args{suffix} = exists $xslate_args{extension}
? delete $xslate_args{extension}
: ".tt";
$xslate_args{suffix} = ".$xslate_args{suffix}"
if $xslate_args{suffix} !~ /^\./;
$self->{driver} = Text::Xslate->new(%xslate_args);
return $self;
}
sub render {
my ($self, $template, $tokens) = @_;
my $app = Dancer::App->current;
$template = abs2rel( rel2abs($template), $app->setting("views") );
my $xslate = $self->{driver};
my $content = $xslate->render($template, $tokens);
if (my $err = $@) {
croak qq(Couldn't render template "$err");
}
return $content;
}
1;
__END__
=head1 DESCRIPTION
This class is an interface between Dancer's template engine abstraction layer
and the L<Text::Xslate> module.
In order to use this engine, use the template setting:
template: xslate
This can be done in your config.yml file or directly in your app code with the
B<set> keyword.
You can configure L<Text::Xslate>:
template: xslate
engines:
xslate:
cache_dir: .xslate_cache/
cache: 1
extension: tx # Dancer's default template extension is "tt"
module:
- Text::Xslate::Bridge::TT2Like # to keep partial compatibility with Template Toolkit
=head1 CAVEATS
=over
=item Cascading
Dancer already provides a <cascade>-like feature, called a "layout", in order
to augment other template engines lacking such a feature. In order to use
Xslate's C<cascade>, turn off C<layout> by commenting out or removing the
appropriate line in your Dancer application config.
=item Smart HTML Escaping
Use of Dancer's C<layout> feature will cause HTML templates to be HTML-entity
encoded twice if Xslate's "smart HTML escaping" feature is enabled. Xslate's
C<type> option can be set to "text" to disable smart-escaping, or, once again,
C<layout> can be disabled in favor of C<cascade>.
=back
=head1 SEE ALSO
=over
=item L<Dancer>
=item L<Text::Xslate>
=back
|