Skip to content

Commit

Permalink
rebase on current devel
Browse files Browse the repository at this point in the history
  • Loading branch information
dams authored and Alexis Sukrieh committed Jan 3, 2011
1 parent 9ae4e2a commit 307a808
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 89 deletions.
20 changes: 18 additions & 2 deletions lib/Dancer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $VERSION = '1.3000_01';
del
dirname
error
engine
false
forward
from_dumper
Expand Down Expand Up @@ -108,6 +109,7 @@ sub content_type { Dancer::Response->content_type(@_) }
sub dance { Dancer::start(@_) }
sub debug { goto &Dancer::Logger::debug }
sub dirname { Dancer::FileUtils::dirname(@_) }
sub engine { Dancer::Helpers::engine(@_) }
sub error { goto &Dancer::Logger::error }
sub send_error { Dancer::Helpers->error(@_) }
sub false {0}
Expand Down Expand Up @@ -447,6 +449,14 @@ Returns the dirname of the path given:
my $dir = dirname($some_path);
=head2 engine
Given an namespace, returns the current engine object
my $template_engine = engine 'template';
my $html = $template_engine->apply_renderer(...);
$template_engine->apply_layout($html);
=head2 error
Logs a message of error level:
Expand Down Expand Up @@ -713,13 +723,19 @@ function, e.g.
Allows a handler to provide plain HTML (or other content), but have it rendered
within the layout still.
For example:
This method is B<DEPRECATED>, and will be removed soon. Instead, you should be
using the C<engine> keyword:
get '/foo' => sub {
# Do something which generates HTML directly (maybe using
# HTML::Table::FromDatabase or something)
my $content = ...;
render_with_layout $content;
# get the template engine
my $template_engine = engine 'template';
# apply the layout (not the renderer), and return the result
$template_engine->apply_layout($content)
};
It works very similarly to C<template> in that you can pass tokens to be used in
Expand Down
17 changes: 14 additions & 3 deletions lib/Dancer/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ sub build {
croak "cannot build engine without type and name "
unless $name and $type;

my $class_name = ucfirst($type);
my $namespace = "Dancer::${class_name}";
my $class_name = $class->_engine_class($type);

$config ||= {};
$config->{engines} ||= {};
my $settings = $config->{engines}{$name} || {};

# trying to load the engine
my $engine_class =
Dancer::ModuleLoader->class_from_setting($namespace => $name);
Dancer::ModuleLoader->class_from_setting($class_name => $name);

croak "unknown $type engine '$name', "
. "perhaps you need to install $engine_class?"
Expand All @@ -56,6 +55,18 @@ sub build {
);
}

sub _engine_class {
my ($class, $type) = @_;
$type = ucfirst($type);
return "Dancer::${type}";
}

sub engine {
my ($class, $type) = @_;
return $class->_engine_class($type)->engine();
}


1;

__END__
Expand Down
82 changes: 33 additions & 49 deletions lib/Dancer/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package Dancer::Helpers;

use strict;
use warnings;
use Carp qw(carp);

use Dancer::Response;
use Dancer::Config 'setting';
Expand All @@ -29,73 +30,56 @@ sub send_file {
Dancer::Response->set($error->render);
}

sub engine {
my ($engine_type) = @_;
Dancer::Engine->engine($engine_type);
}

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

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
# 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 ||= {};
$tokens->{dancer_version} = $Dancer::VERSION;
$tokens->{settings} = Dancer::Config->settings;
$tokens->{request} = Dancer::SharedData->request;
$tokens->{params} = Dancer::SharedData->request->params;

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

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

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

$_->($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);
}
$options ||= {};
$content = delete $options->{content};
}

my $full_content =
Dancer::Template->engine->layout($layout, $tokens, $content);
return $full_content;
my $full_content = Dancer::Template->engine->apply_layout($content, $tokens, $options);
defined $full_content
and return $full_content;

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

# DEPRECATED
sub render_with_layout {
my ($content, $tokens, $options) = @_;
$options ||= {};
$options->{content} = $content;
return template('', $tokens, $options);
carp "'render_with_layout' is DEPRECATED, use the 'engine' keyword to get the template engine, and use 'apply_layout' on the result";

my $full_content = Dancer::Template->engine->apply_layout($content, $tokens, $options);

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

sub error {
Expand Down
25 changes: 16 additions & 9 deletions lib/Dancer/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ sub get_attributes {
return \@attributes;
}

# accessor code for normal objects
# (overloaded in D::O::Singleton for instance)
sub _setter_code {
my ($class, $attr) = @_;
sub {
my ($self, $value) = @_;
if (@_ == 1) {
return $self->{$attr};
}
else {
return $self->{$attr} = $value;
}
};
}

# accessors builder
sub attributes {
my ($class, @attributes) = @_;
Expand All @@ -67,15 +82,7 @@ sub attributes {

# define setters and getters for each attribute
foreach my $attr (@attributes) {
my $code = sub {
my ($self, $value) = @_;
if (@_ == 1) {
return $self->{$attr};
}
else {
return $self->{$attr} = $value;
}
};
my $code = $class->_setter_code($attr);
my $method = "${class}::${attr}";
{ no strict 'refs'; *$method = $code; }
}
Expand Down
17 changes: 17 additions & 0 deletions lib/Dancer/Object/Singleton.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ sub instance {
return $instance;
}

# accessor code for singleton objects
# (overloaded from Dancer::Object)
sub _setter_code {
my ($class, $attr) = @_;
sub {
my ($class_or_instance, $value) = @_;
my $instance = ref $class_or_instance ?
$class_or_instance : $class_or_instance->instance;
if (@_ == 1) {
return $instance->{$attr};
}
else {
return $instance->{$attr} = $value;
}
};
}

1;

__END__
Expand Down
11 changes: 7 additions & 4 deletions lib/Dancer/Serializer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ use Dancer::SharedData;
my $_engine;

sub engine {
return $_engine if $_engine;

$_engine
and return $_engine;
# don't create a new serializer unless it's defined in the config
# (else it's created using json, and that's *not* what we want)
my $serializer = Dancer::App->current->setting('serializer');
Dancer::Serializer->init($serializer) if $serializer;
my $serializer_name = Dancer::App->current->setting('serializer');
$serializer_name
and return Dancer::Serializer->init($serializer_name);
return;
}

sub init {
my ($class, $name, $config) = @_;
$name ||= 'JSON';
$_engine = Dancer::Engine->build('serializer' => $name, $config);
return $_engine;
}

# takes a response object and checks whether or not it should be
Expand Down
11 changes: 5 additions & 6 deletions lib/Dancer/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use Dancer::ModuleLoader;
use Dancer::Engine;

# singleton for the current template engine
my $engine;
sub engine {$engine}
my $_engine;
sub engine { $_engine }

# init the engine according to the settings
# the template engine module will take from the
# setting name.
# init the engine according to the settings the template engine module will
# take from the setting name.
sub init {
my ($class, $name, $config) = @_;
$name ||= 'simple';
$engine = Dancer::Engine->build(template => $name, $config);
$_engine = Dancer::Engine->build(template => $name, $config);
}

1;
Expand Down
Loading

0 comments on commit 307a808

Please sign in to comment.