summaryrefslogblamecommitdiff
path: root/lib/Dancer/Template/Xslate.pm
blob: 203089d437f5bb6bac5bab9fdcd4a590e7388d1b (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                 



             
         
                

                                        


                                      

                                           
 

                                                   
 
          



                                      
 
                                              














                                                                            



                                        
                               
 



                                                                  

                       
                                                  






















                                                                              




                                                                     




                                                 
package Dancer::Template::Xslate;


use strict;
use warnings;

use Carp;
use Dancer::App;
use File::Spec::Functions qw(splitpath);
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 $config = $self->config;
    my $app    = Dancer::App->current;
    my %xslate_args = %{$config};

    ## set default path for header/footer etc.
    $xslate_args{path} ||= [];
    my $views_dir = $config->{views_dir} = $app->setting('views');
    push @{$xslate_args{path}}, $views_dir
        if not grep { $_ eq $views_dir } @{$xslate_args{path}};

    ## for those who read Text::Xslate instead of Dancer::Template::Abstract
    $config->{extension} = $xslate_args{suffix}
        if exists $xslate_args{suffix};

    ## avoid 'Text::Xslate: Unknown option(s): extension'
    $xslate_args{suffix} = delete $xslate_args{extension}
        if exists $xslate_args{extension};

    $self->{driver} = Text::Xslate->new(%xslate_args);
    return;
}

sub render {
    my ($self, $template, $tokens) = @_;
    my $config = $self->config;

    my $views_dir = $self->config->{views_dir};
    (undef, undef, $template) = splitpath $template if $views_dir;
    my $xslate = $self->{driver};
    my $content = $xslate->render($template, $tokens);

    if (my $err = $@) {
        croak qq[Couldn't render template "$err"];
    }

    return $content;
}

1;

=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  => "/www/../xslate_cache",
        cache      => 1,
        module =>
          - Text::Xslate::Bridge::TT2 # to keep partial compatibility


=head1 SEE ALSO

L<Dancer>, L<Text::Xslate>, L<http://xslate.org/>