Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TT's INCLUDE_PATH always point to app's views directory #1009

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ t/10_template/views/index.ts
t/10_template/views/index.tt
t/10_template/views/layouts/main.ts
t/10_template/views/layouts/main.tt
t/10_template/views2/composite.tt
t/10_template/views2/includes/header.tt
t/11_logger/000_create_fake_env.t
t/11_logger/01_abstract.t
t/11_logger/02_factory.t
Expand Down
12 changes: 9 additions & 3 deletions lib/Dancer/Template/TemplateToolkit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Carp;
use Dancer::Config 'setting';
use Dancer::ModuleLoader;
use Dancer::Exception qw(:all);

use Scalar::Util qw(weaken);
use base 'Dancer::Template::Abstract';

my $_engine;
Expand All @@ -26,9 +26,15 @@ sub init {
my @anycase = $is_subclass ? () : ( ANYCASE => 1 );
my @absolute = $is_subclass ? () : ( ABSOLUTE => 1 );

weaken(my $copy_self = $self);
my @inc_path = $is_subclass ? ()
: ( INCLUDE_PATH => $self->config->{INCLUDE_PATH} || setting('views') );

: ( INCLUDE_PATH => [ sub {
my $delim = $copy_self->config->{DELIMITER};
# tweak delim to ignore C:/
$delim //= $^O eq 'MSWin32' ? ':(?!\\/)' : ':';
[ split /$delim/, $copy_self->config->{INCLUDE_PATH} || setting('views') ] }
] );

my $start_tag = $is_subclass
? $self->config->{start_tag}
: $self->config->{start_tag} || '<%';
Expand Down
13 changes: 10 additions & 3 deletions t/10_template/05_template_toolkit.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Test::More;
use Test::More import => ['!pass'];


use strict;
Expand All @@ -9,11 +9,13 @@ use Dancer::FileUtils 'path';
use File::Spec;
use lib File::Spec->catdir( 't', 'lib' );
use EasyMocker;
use Dancer;


BEGIN {
plan skip_all => "need Template to run this test"
unless Dancer::ModuleLoader->load('Template');
plan tests => 7;
plan tests => 8;
use_ok 'Dancer::Template::TemplateToolkit';
};

Expand All @@ -36,7 +38,7 @@ is $@, '',
"Template dependency is not triggered if Template is there";

# as a file path
my $template = path('t', '10_template', 'index.txt');
my $template = File::Spec->rel2abs(path('t', '10_template', 'index.txt'));
my $result = $engine->render(
$template,
{ var1 => 1,
Expand Down Expand Up @@ -71,3 +73,8 @@ like $@, qr/doesn't exist or not a regular file/, "prorotype failure detected";

$result = $engine->render(\$template, { one => 1, two => 2, three => 3});
is $result, $expected, "processed a template given as a scalar ref";

setting views => path(setting('appdir'), 'views2');
$template = File::Spec->rel2abs(path('t', '10_template', 'views2', 'composite.tt'));
$result = $engine->render($template);
is $result, 'foo bar', "changed views directory and processed a template with includes";
1 change: 1 addition & 0 deletions t/10_template/views2/composite.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% INCLUDE "includes/header.tt" %> bar
1 change: 1 addition & 0 deletions t/10_template/views2/includes/header.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo