Skip to content

Commit

Permalink
Merge branch 'feature/global-template-keyword'
Browse files Browse the repository at this point in the history
  • Loading branch information
veryrusty committed May 5, 2016
2 parents 1e97ed3 + 82656cf commit 5d3cdca
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[ ENHANCEMENTS ]
* GH #1145, #1164: Replace Class::Load with Module::Runtime
(Sawyer X)
* GH #1159, #1163: Make template keyword global
(Sawyer X, Russell Jenkins)

0.166001_02 2016-04-29 16:42:54+02:00 Europe/Amsterdam (TRIAL RELEASE)

Expand Down
4 changes: 3 additions & 1 deletion lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,12 @@ sub template {
my $template = $self->template_engine;
$template->set_settings( $self->config );

# A session will not exist if there is no request (global keyword)
#
# A session may exist but the route code may not have instantiated
# the session object (sessions are lazy). If this is the case, do
# that now, so the templates have the session data for rendering.
$self->has_session && ! $template->has_session
$self->has_request && $self->has_session && ! $template->has_session
and $self->setup_session;

# return content
Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer2/Core/DSL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ sub dsl_keywords {
splat => { is_global => 0 },
start => { is_global => 1 },
status => { is_global => 0 },
template => { is_global => 0 },
template => { is_global => 1 },
to_app => { is_global => 1 },
to_dumper => { is_global => 1 },
to_json => { is_global => 1 },
Expand Down
16 changes: 10 additions & 6 deletions lib/Dancer2/Core/Role/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,18 @@ sub _prepare_tokens_options {
$tokens ||= {};
$tokens->{perl_version} = $^V;
$tokens->{dancer_version} = Dancer2->VERSION;
$tokens->{settings} = $self->settings;

$tokens->{settings} = $self->settings;
$tokens->{request} = $self->request;
$tokens->{params} = $self->request->params;
$tokens->{vars} = $self->request->vars;
# no request when template is called as a global keyword
if ( $self->has_request ) {
$tokens->{request} = $self->request;
$tokens->{params} = $self->request->params;
$tokens->{vars} = $self->request->vars;

$tokens->{session} = $self->session->data
if $self->has_session;
# a session can not exist if there is no request
$tokens->{session} = $self->session->data
if $self->has_session;
}

return $tokens;
}
Expand Down
8 changes: 8 additions & 0 deletions t/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ $tt->add_hook(
Dancer2->runner->apps->[0]->set_template_engine($tt);

get '/' => sub { template index => { var => 42 } };

# Call template as a global keyword
my $global= template( index => { var => 21 } );
get '/global' => sub { $global };
}

subtest 'template hooks' => sub {
Expand All @@ -98,6 +102,10 @@ content added in after_layout_render";
my $test = Plack::Test->create( Bar->to_app );
my $res = $test->request( GET '/' );
is $res->content, $result, '[GET /] Correct content with template hooks';

$result =~ s/42/21/g;
$res = $test->request( GET '/global' );
is $res->content, $result, '[GET /global] Correct content with template hooks';
};

{
Expand Down

0 comments on commit 5d3cdca

Please sign in to comment.