| 1 | package Dancer2::Template::Xslate; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use v5.8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Dancer2::Core::Types qw(InstanceOf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Text::Xslate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use File::Spec::Functions qw(abs2rel file_name_is_absolute); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | our $VERSION = 'v0.2.0'; # VERSION | |||||||||
| 16 | # ABSTRACT: Text::Xslate template engine for Dancer2 | ||||||||||
| 17 | |||||||||||
| 18 | 2 | with 'Dancer2::Core::Role::Template'; | |||||||||
| 19 | |||||||||||
| 20 | has '+default_tmpl_ext' => ( | ||||||||||
| 21 | 1 | ▶ | default => sub { 'tx' } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ); | |||||||||
| 23 | 2 | has '+engine' => ( | |||||||||
| 24 | isa => InstanceOf['Text::Xslate'] | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | sub _build_engine { | ||||||||
|
Subroutine
_build_engine: called CC=3 SCAR=13.0
| |||||||||||
| 28 | 1 | my ($self) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 1 | my %config = %{ $self->config }; | |||||||||
| 31 | |||||||||||
| 32 | # Dancer2 injects a couple options without asking; Text::Xslate protests: | ||||||||||
| 33 | 1 | delete $config{environment}; | |||||||||
| 34 | 1 | ▶ | if ( my $location = delete $config{location} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | ▶ | unless (defined $config{path}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | $config{path} = [$location]; | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | return Text::Xslate->new(%config); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 2 | ▶ | sub render { | ||||||||
|
Subroutine
render: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||
| 44 | 2 | my ($self, $tmpl, $vars) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 2 | my $xslate = $self->engine; | |||||||||
| 47 | 2 | my $content = eval { | |||||||||
| 48 | 2 | ▶ | if ( ref($tmpl) eq 'SCALAR' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | $xslate->render_string($$tmpl, $vars) | |||||||||
| 50 | } | ||||||||||
| 51 | else { | ||||||||||
| 52 | my $rel_path = file_name_is_absolute($tmpl) | ||||||||||
| 53 | ? abs2rel($tmpl, $self->config->{location}) | ||||||||||
| 54 | 2 | ▶ | : $tmpl; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 2 | $xslate->render($rel_path, $vars); | |||||||||
| 56 | } | ||||||||||
| 57 | }; | ||||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | $@ and croak $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 2 | return $content; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 2 | 1; | |||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | =encoding utf8 | ||||||||||
| 68 | |||||||||||
| 69 | =head1 NAME | ||||||||||
| 70 | |||||||||||
| 71 | Dancer2::Template::Xslate - Text::Xslate template engine for Dancer2 | ||||||||||
| 72 | |||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||
| 74 | |||||||||||
| 75 | C<config.yaml>: | ||||||||||
| 76 | |||||||||||
| 77 | template: Xslate | ||||||||||
| 78 | |||||||||||
| 79 | A Dancer 2 application: | ||||||||||
| 80 | |||||||||||
| 81 | use Dancer2; | ||||||||||
| 82 | |||||||||||
| 83 | get '/page/:number' => sub { | ||||||||||
| 84 | my $page_num = params->{number}; | ||||||||||
| 85 | template "foo.tx", { page_num => $page_num }; | ||||||||||
| 86 | }; | ||||||||||
| 87 | |||||||||||
| 88 | If you want to use cascading templates to manage page layouts | ||||||||||
| 89 | make sure you disable Dancer2 layouts. | ||||||||||
| 90 | |||||||||||
| 91 | In C<config.yaml>, comment the C<layout> keyword: | ||||||||||
| 92 | |||||||||||
| 93 | # The default layout to use for your application (located in | ||||||||||
| 94 | # views/layouts/main.tt) | ||||||||||
| 95 | # layout: "main" | ||||||||||
| 96 | |||||||||||
| 97 | If you don't, Dancer2 will render your template and then render | ||||||||||
| 98 | the C<layout> template. If your layout template doesn't have the | ||||||||||
| 99 | C<content> placeholder only the layout HTML will be returned. | ||||||||||
| 100 | |||||||||||
| 101 | =head1 METHODS | ||||||||||
| 102 | |||||||||||
| 103 | =over | ||||||||||
| 104 | |||||||||||
| 105 | =item render($template, $tokens) | ||||||||||
| 106 | |||||||||||
| 107 | Renders a template. C<$template> can be one of: | ||||||||||
| 108 | |||||||||||
| 109 | =over 2 | ||||||||||
| 110 | |||||||||||
| 111 | =item * | ||||||||||
| 112 | |||||||||||
| 113 | a string of the path to a template file (*.tx, not *.tt like the core Dancer2 | ||||||||||
| 114 | template engines) | ||||||||||
| 115 | |||||||||||
| 116 | =item * | ||||||||||
| 117 | |||||||||||
| 118 | a reference to a string containing prerendered template content | ||||||||||
| 119 | |||||||||||
| 120 | =back | ||||||||||
| 121 | |||||||||||
| 122 | =back | ||||||||||
| 123 | |||||||||||
| 124 | =head1 SEE ALSO | ||||||||||
| 125 | |||||||||||
| 126 | =over | ||||||||||
| 127 | |||||||||||
| 128 | =item L<Dancer::Template::Xslate> | ||||||||||
| 129 | |||||||||||
| 130 | Xslate rendering engine for Dancer 1. | ||||||||||
| 131 | |||||||||||
| 132 | =back | ||||||||||
| 133 | |||||||||||
| 134 | =head1 AUTHOR | ||||||||||
| 135 | |||||||||||
| 136 | Richard Simões C<< <rsimoes AT cpan DOT org> >> | ||||||||||
| 137 | |||||||||||
| 138 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 139 | |||||||||||
| 140 | Copyright © 2013 Richard Simões. This module is released under the terms of the | ||||||||||
| 141 | B<MIT License> and may be modified and/or redistributed under the same or any | ||||||||||
| 142 | compatible license. | ||||||||||