Skip to content

Commit

Permalink
Revert ". template is now 'apply_renderer' + 'apply_layout'"
Browse files Browse the repository at this point in the history
This reverts commit 7b881c4.
  • Loading branch information
fcuny committed Nov 30, 2010
1 parent d560787 commit 68caccd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 154 deletions.
81 changes: 21 additions & 60 deletions lib/Dancer.pm
Expand Up @@ -35,8 +35,6 @@ $VERSION = '1.2000';
@EXPORT = qw(
after
any
apply_layout
apply_renderer
before
before_template
cookies
Expand Down Expand Up @@ -100,8 +98,6 @@ $VERSION = '1.2000';

sub after { Dancer::Route::Registry->hook('after', @_) }
sub any { Dancer::App->current->registry->any_add(@_) }
sub apply_layout { Dancer::Helpers::apply_layout(@_) }
sub apply_renderer { Dancer::Helpers::apply_renderer(@_) }
sub before { Dancer::Route::Registry->hook('before', @_) }
sub before_template { Dancer::Route::Registry->hook('before_template', @_) }
sub captures { Dancer::SharedData->request->params->{captures} }
Expand Down Expand Up @@ -373,55 +369,6 @@ Or even, a route handler that would match any HTTP methods:
# code
};
=head2 apply_layout
It is the second part of what C<template> does internally. Basically,
C<template> is C<apply_renderer> + C<apply_layout>.
C<apply_layout> allows a handler to provide plain HTML (or other content), but
have it rendered within the layout still.
For example:
get '/foo' => sub {
# Do something which generates HTML directly (maybe using
# HTML::Table::FromDatabase or something)
my $content = ...;
apply_layout $content;
};
You can pass tokens to be used in the layout, and/or options to control the way
the layout is rendered. For instance, to set a 'foo' token to 'bar', and use a
custom layout:
apply_layout $content, { foo => 'bar' }, { layout => 'layoutname' };
Remember, C<apply_layout> doesn't generates the content, it only applies the
layout around it. So, C<$content> should be something semi final, like HTML. If
you need to generate the content from a view, you should use C<template>, or
C<apply_renderer>.
=head2 apply_renderer
It is the first part of what C<template> does internally. Basically,
C<template> is C<apply_renderer> + C<apply_layout>.
C<apply_renderer> generates content from a view, but doesn't apply the layout.
For example:
get '/foo' => sub {
my $content = apply_renderer('home');
# $content now contains the rendered 'home' view
# .. do something with $content
};
You can pass tokens to be used in the view. For instance, to set a 'foo' token
to 'bar':
apply_renderer $content, { foo => 'bar'};
=head2 before
Defines a before filter:
Expand Down Expand Up @@ -732,7 +679,24 @@ function, e.g.
=head2 render_with_layout
B<DEPRECATED> : this keyword is B<deprecated>. Please use C<apply_layout> instead.
Allows a handler to provide plain HTML (or other content), but have it rendered
within the layout still.
For example:
get '/foo' => sub {
# Do something which generates HTML directly (maybe using
# HTML::Table::FromDatabase or something)
my $content = ...;
render_with_layout $content;
};
It works very similarly to C<template> in that you can pass tokens to be used in
the layout, and/or options to control the way the layout is rendered. For
instance, to use a custom layout:
render_with_layout $content, {}, { layout => 'layoutname' };
=head2 request
Expand Down Expand Up @@ -861,9 +825,7 @@ underscores as a separator for blanks.
=head2 template
Basically, C<template> is C<apply_renderer> + C<apply_layout>.
C<template> tells the route handler to build a response with the current template engine:
Tells the route handler to build a response with the current template engine:
get '/' => sub {
...
Expand All @@ -874,11 +836,10 @@ The first parameter should be a template available in the views directory, the
second one (optional) is a hashref of tokens to interpolate, and the third
(again optional) is a hashref of options.
For example, to set the 'foo' token to 'bar', but disable the layout for a
specific request:
For example, to disable the layout for a specific request:
get '/' => sub {
template 'index.tt', { foo => bar }, { layout => undef };
template 'index.tt', {}, { layout => undef };
};
Expand Down
128 changes: 44 additions & 84 deletions lib/Dancer/Helpers.pm
Expand Up @@ -6,7 +6,6 @@ package Dancer::Helpers;

use strict;
use warnings;
use Carp;

use Dancer::Response;
use Dancer::Config 'setting';
Expand All @@ -33,72 +32,7 @@ sub send_file {
sub template {
my ($view, $tokens, $options) = @_;

($tokens, $options, my $app) = _prepare_tokens_options($tokens, $options);

my $content;
if ($view) {
$content = _apply_renderer($view, $tokens, $options, $app);
if (! defined $content) {
my $error = Dancer::Error->new(
code => 404,
message => "Page not found",
);
return Dancer::Response::set($error->render);
}
} else {
$content = delete $options->{content};
}

my $full_content = _apply_layout($content, $tokens, $options, $app);
defined $full_content
and return $full_content;

my $error = Dancer::Error->new(
code => 404,
message => "Page not found",
);
return Dancer::Response::set($error->render);

}

sub apply_renderer {
my ($view, $tokens, $options) = @_;
return _apply_renderer($view, _prepare_tokens_options($tokens, $options));
}

sub _apply_renderer {
my ($view, $tokens, $options, $app) = @_;

$view = Dancer::Template->engine->view($view);
-r $view or return;

$_->($tokens) for (@{$app->registry->hooks->{before_template}});

my $content = Dancer::Template->engine->render($view, $tokens);
return $content;
}

# new name :
sub apply_layout {
my ($content, $tokens, $options) = @_;
return _apply_layout($content, _prepare_tokens_options($tokens, $options));
}

sub render_with_layout {
carp "'render_with_layout' is DEPRECATED, use 'apply_layout' instead";
my $content = Dancer::Helpers::apply_layout(@_);
if (! defined $content) {
my $error = Dancer::Error->new(
code => 404,
message => "Page not found",
);
return Dancer::Response::set($error->render);
}
return $content;
}

sub _apply_layout {
my ($content, $tokens, $options, $app) = @_;
my $app = Dancer::App->current;

# 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
Expand All @@ -109,19 +43,6 @@ sub _apply_layout {
? ($options->{layout} ? $options->{layout} : undef)
: $app->setting('layout');

defined $content or return;

defined $layout or return $content;

my $full_content =
Dancer::Template->engine->layout($layout, $tokens, $content);
return $full_content;
}

sub _prepare_tokens_options {
my ($tokens, $options) = @_;

$options ||= {};

# these are the default tokens provided for template processing
$tokens ||= {};
Expand All @@ -130,12 +51,51 @@ sub _prepare_tokens_options {
$tokens->{request} = Dancer::SharedData->request;
$tokens->{params} = Dancer::SharedData->request->params;

setting('session')
and $tokens->{session} = Dancer::Session->get;
if (setting('session')) {
$tokens->{session} = Dancer::Session->get;
}

my $app = Dancer::App->current;
my $content;
if ($view) {
$view = Dancer::Template->engine->view($view);

if (!-r $view) {
my $error = Dancer::Error->new(
code => 404,
message => "Page not found",
);
return Dancer::Response::set($error->render);
}

return ($tokens, $options, $app);
$_->($tokens) for (@{$app->registry->hooks->{before_template}});

$content = Dancer::Template->engine->render($view, $tokens);
return $content if not defined $layout;
} else {
# No view name specified; look for an option named content, and,
# if found, use that as the content, putting the layout around it.
if (exists $options->{content}) {
$content = delete $options->{content};
return $content if not defined $layout;
} else {
my $error = Dancer::Error->new(
code => 404,
message => "Page not found",
);
return Dancer::Response::set($error->render);
}
}

my $full_content =
Dancer::Template->engine->layout($layout, $tokens, $content);
return $full_content;
}

sub render_with_layout {
my ($content, $tokens, $options) = @_;
$options ||= {};
$options->{content} = $content;
return template('', $tokens, $options);
}

sub error {
Expand Down
20 changes: 10 additions & 10 deletions t/05_views/03_layout.t
Expand Up @@ -23,11 +23,11 @@ my @tests = (
expected => "view\n" },
{ path => '/layoutchanged',
expected => "customstart\nview\ncustomstop\n" },
{ path => '/apply_layout/default_layout',
{ path => '/render_with_layout/default_layout',
expected => "start\ncontent\nstop\n" },
{ path => '/apply_layout/no_layout',
{ path => '/render_with_layout/no_layout',
expected => "content\n" },
{ path => '/apply_layout/custom_layout',
{ path => '/render_with_layout/custom_layout',
expected => "customstart\ncontent\ncustomstop\n" },
);

Expand All @@ -54,18 +54,18 @@ SKIP: {
template 't03', {}, { layout => 'custom' };
};

get '/apply_layout/default_layout' => sub {
apply_layout("content\n");
get '/render_with_layout/default_layout' => sub {
render_with_layout("content\n");
};

# Yes, apply_layout without a layout is kind of pointless, but let's
# Yes, render_with_layout without a layout is kind of pointless, but let's
# be thorough :)
get '/apply_layout/no_layout' => sub {
apply_layout("content\n", {}, { layout => undef });
get '/render_with_layout/no_layout' => sub {
render_with_layout("content\n", {}, { layout => undef });
};

get '/apply_layout/custom_layout' => sub {
apply_layout("content\n", {}, { layout => 'custom' });
get '/render_with_layout/custom_layout' => sub {
render_with_layout("content\n", {}, { layout => 'custom' });
};

foreach my $test (@tests) {
Expand Down

0 comments on commit 68caccd

Please sign in to comment.