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