Skip to content

Commit

Permalink
better renderer and condition examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 7, 2012
1 parent 993b9e6 commit 2f71fc2
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -882,7 +882,7 @@ do is add a new C<handler>.

app->renderer->add_handler(
mine => sub {
my ($r, $c, $output, $options) = @_;
my ($renderer, $c, $output, $options) = @_;

# Check for one time use inline template
my $inline = $options->{inline};
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -713,7 +713,7 @@ play, they are basically router plugins.
# Simple "User-Agent" condition
$r->add_condition(
agent => sub {
my ($r, $c, $captures, $pattern) = @_;
my ($route, $c, $captures, $pattern) = @_;

# User supplied regular expression
return unless $pattern && ref $pattern eq 'Regexp';
Expand Down Expand Up @@ -750,7 +750,7 @@ You can also package your conditions as reusable plugins.
# Add "werewolf" condition
$app->routes->add_condition(
werewolf => sub {
my ($r, $c, $captures, $days) = @_;
my ($route, $c, $captures, $days) = @_;

# Keep the werewolfs out!
return if abs(14 - (phase(time))[2]) > ($days / 2);
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious/Plugin/EPLRenderer.pm
Expand Up @@ -12,16 +12,16 @@ sub register {
}

sub _epl {
my ($r, $c, $output, $options) = @_;
my ($renderer, $c, $output, $options) = @_;

# Template
my $inline = $options->{inline};
my $path = $r->template_path($options);
my $path = $renderer->template_path($options);
$path = md5_sum encode('UTF-8', $inline) if defined $inline;
return unless defined $path;

# Cached
my $cache = $r->cache;
my $cache = $renderer->cache;
my $key = delete $options->{cache} || $path;
my $mt = $cache->get($key) || Mojo::Template->new;
if ($mt->compiled) {
Expand All @@ -41,8 +41,8 @@ sub _epl {

# File
else {
$mt->encoding($r->encoding) if $r->encoding;
return unless my $t = $r->template_name($options);
$mt->encoding($renderer->encoding) if $renderer->encoding;
return unless my $t = $renderer->template_name($options);

# Try template
if (-r $path) {
Expand All @@ -52,7 +52,7 @@ sub _epl {
}

# Try DATA section
elsif (my $d = $r->get_data_template($options)) {
elsif (my $d = $renderer->get_data_template($options)) {
$c->app->log->debug(qq{Rendering template "$t" from DATA section.});
$mt->name(qq{template "$t" from DATA section});
$$output = $mt->render($d, $c);
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -14,16 +14,16 @@ sub register {
# Add "ep" handler
$app->renderer->add_handler(
$conf->{name} || 'ep' => sub {
my ($r, $c, $output, $options) = @_;
my ($renderer, $c, $output, $options) = @_;

# Generate name
my $path = $options->{inline} || $r->template_path($options);
my $path = $options->{inline} || $renderer->template_path($options);
return unless defined $path;
my $id = encode 'UTF-8', join(', ', $path, sort keys %{$c->stash});
my $key = $options->{cache} = md5_sum $id;

# Compile helpers and stash values
my $cache = $r->cache;
my $cache = $renderer->cache;
unless ($cache->get($key)) {
my $mt = Mojo::Template->new($template);

Expand All @@ -34,7 +34,7 @@ sub register {
# Helpers
$prepend .= 'my $_H = $self->app->renderer->helpers;';
$prepend .= "sub $_; *$_ = sub { \$_H->{'$_'}->(\$self, \@_) };"
for grep {/^\w+$/} keys %{$r->helpers};
for grep {/^\w+$/} keys %{$renderer->helpers};

# Be less relaxed for everything else
$prepend .= 'use strict;';
Expand All @@ -49,7 +49,7 @@ sub register {
}

# Render with "epl" handler
return $r->handlers->{epl}->($r, $c, $output, $options);
return $renderer->handlers->{epl}->($renderer, $c, $output, $options);
}
);

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/HeaderCondition.pm
Expand Up @@ -24,7 +24,7 @@ sub _check {
}

sub _headers {
my ($r, $c, $captures, $patterns) = @_;
my ($route, $c, $captures, $patterns) = @_;
return unless $patterns && ref $patterns eq 'HASH' && keys %$patterns;

# All headers need to match
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -18,10 +18,11 @@ sub register {
my $preprocess = $conf->{preprocess} || 'ep';
$app->renderer->add_handler(
$conf->{name} || 'pod' => sub {
my ($r, $c, $output, $options) = @_;
my ($renderer, $c, $output, $options) = @_;

# Preprocess and render
return unless $r->handlers->{$preprocess}->($r, $c, $output, $options);
my $handler = $renderer->handlers->{$preprocess};
return unless $handler->($renderer, $c, $output, $options);
$$output = _pod_to_html($$output);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/charset_lite_app.t
Expand Up @@ -23,7 +23,7 @@ plugin Charset => {charset => 'Shift_JIS'};
# UTF-8 text renderer
app->renderer->add_handler(
test => sub {
my ($r, $c, $output, $options) = @_;
my ($renderer, $c, $output, $options) = @_;
delete $options->{encoding};
$$output = b($c->stash->{test})->encode('UTF-8')->to_string;
}
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -73,7 +73,7 @@ hook after_static_dispatch => sub {
# Response generating condition "res" for /res.txt
app->routes->add_condition(
res => sub {
my ($r, $c) = @_;
my ($route, $c) = @_;
return 1 unless $c->param('res');
$c->tx->res(
Mojo::Message::Response->new(code => 201)->body('Conditional response!')
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/external/myapp.pl
Expand Up @@ -13,7 +13,7 @@
# Message condition
app->routes->add_condition(
message => sub {
my ($r, $c, $captures, $msg) = @_;
my ($route, $c, $captures, $msg) = @_;
$c->res->headers->header('X-Message' => $msg);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -473,7 +473,7 @@ get '/captures/:foo/:bar' => sub {
# Default condition
app->routes->add_condition(
default => sub {
my ($r, $c, $captures, $num) = @_;
my ($route, $c, $captures, $num) = @_;
$captures->{test} = $captures->{text} . "$num works!";
return 1 if $c->stash->{default} == $num;
return;
Expand All @@ -491,7 +491,7 @@ get '/default/:text' => (default => 23) => sub {
# Redirect condition
app->routes->add_condition(
redirect => sub {
my ($r, $c, $captures, $active) = @_;
my ($route, $c, $captures, $active) = @_;
return 1 unless $active;
$c->redirect_to('index') and return
unless $c->req->headers->header('X-Condition-Test');
Expand Down

0 comments on commit 2f71fc2

Please sign in to comment.