Skip to content

Commit

Permalink
Allow the layout option to template() to set custom layout.
Browse files Browse the repository at this point in the history
Make the 'layout' option to the template keyword do what you'd expect, and
support setting a custom layout, for instance:

  template 'templatename', {}, { layout => 'layoutname' };

You can still use it as it was used before to disable layouts:

  template 'templatename', {}, { layout => undef };

New tests added which pass, so I'm confident this shouldn't cause breakage.
  • Loading branch information
bigpresh committed Oct 12, 2010
1 parent 364e178 commit 2f7b71d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Expand Up @@ -38,6 +38,12 @@ Dancer 1.1902 (1.2RC2)
[ Damien Krotkine ]
* FIX for issue GH#115
documentation about compression in Dancer::Deployment

[ David Precious ]
* Make the 'layout' param to the template keyword work as you'd expect and
allow you to set a custom layout to use, e.g.:
template 'templatename', {}, { layout => 'layoutname' };


Dancer 1.1901 (1.2RC1)

Expand Down
10 changes: 7 additions & 3 deletions lib/Dancer/Helpers.pm
Expand Up @@ -34,9 +34,13 @@ sub template {

my $app = Dancer::App->current;

$options ||= {layout => 1};
my $layout = $app->setting('layout');
undef $layout unless $options->{layout};
# If 'layout' was given in the options hashref, use it if it's a true value,
# or don't use a layout if it was false (0, or undef); if layout wasn't
# given in the options hashref, go with whatever the current layout setting
# is.
my $layout = exists $options->{layout} ?
($options->{layout} ? $options->{layout} : undef) : $app->setting('layout');


# these are the default tokens provided for template processing
$tokens ||= {};
Expand Down
13 changes: 13 additions & 0 deletions t/05_views/03_layout.t
Expand Up @@ -17,6 +17,10 @@ my @tests = (
expected => "view\n" },
{ path => '/full',
expected => "start\nview\nstop\n" },
{ path => '/layoutdisabled',
expected => "view\n" },
{ path => '/layoutchanged',
expected => "customstart\nview\ncustomstop\n" },
);

plan tests => scalar(@tests);
Expand All @@ -33,6 +37,15 @@ SKIP: {
template 't03';
};

get '/layoutdisabled' => sub {
layout 'main';
template 't03', {}, { layout => undef };
};

get '/layoutchanged' => sub {
template 't03', {}, { layout => 'custom' };
};

foreach my $test (@tests) {
my $path = $test->{path};
my $expected = $test->{expected};
Expand Down
2 changes: 2 additions & 0 deletions t/05_views/views/layouts/custom.tt
@@ -0,0 +1,2 @@
customstart
<% content %>customstop

1 comment on commit 2f7b71d

@sukria
Copy link
Member

@sukria sukria commented on 2f7b71d Oct 13, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Please sign in to comment.