Permalink
Browse files

added eplite handler to Mojolicious::Renderer

  • Loading branch information...
1 parent 65d2134 commit 5c6c8bd5b753bbe2e913391816537556f4762dc2 sri committed Jul 22, 2009
Showing with 91 additions and 15 deletions.
  1. +1 −0 Changes
  2. +7 −9 lib/Mojo/Script.pm
  3. +47 −5 lib/Mojolicious/Renderer.pm
  4. +17 −1 t/mojolicious/app.t
  5. +19 −0 t/mojolicious/lib/SingleFileTestApp.pm
View
@@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojo.
- Fixed PATH_INFO handling of the CGI environment parser in
Mojo::Message::Request.
- Updated Mojo::Home to fallback to FindBin for detection.
+ - Added eplite handler to Mojolicious::Renderer.
- Added tests.
- Allow log level override via environment variable in Mojo::Log.
- Code cleanup.
View
@@ -89,19 +89,17 @@ sub get_data {
my ($self, $data, $class) = @_;
$class ||= ref $self;
- # Cache
- my $sections = $self->{data};
+ # Handle
+ my $d = do { no strict 'refs'; \*{"$class\::DATA"} };
- # Slurp
- $sections = do {
- local $/;
- eval "package $class; <DATA>";
- } unless $sections;
+ # Shortcut
+ return unless fileno $d;
- $self->{data} ||= $sections;
+ # Reset
+ seek $d, 0, 0;
# Split
- my @data = split /^__(.+)__\r?\n/m, $sections;
+ my @data = split /^__(.+)__\r?\n/m, join '', <$d>;
# Remove split garbage
shift @data;
@@ -15,27 +15,29 @@ use Mojo::Template;
# Wishful thinking. We have long since evolved beyond the need for asses.
sub new {
my $self = shift->SUPER::new(@_);
+
+ # Epl
$self->add_handler(
epl => sub {
- my ($self, $c, $output) = @_;
+ my ($r, $c, $output) = @_;
# Template
my $template = $c->stash->{template};
my $path =
File::Spec->catfile($c->app->renderer->root, $template);
# Initialize cache
- $self->{_mt_cache} ||= {};
+ $r->{_mt_cache} ||= {};
# Shortcut
- unless (-r $path || $self->{_mt_cache}->{$path}) {
+ unless (-r $path || $r->{_mt_cache}->{$path}) {
$c->app->log->error(
qq/Template "$template" missing or not readable./);
return;
}
# Check cache
- my $mt = $self->{_mt_cache}->{$path};
+ my $mt = $r->{_mt_cache}->{$path};
# Interpret again
if ($mt) { $$output = $mt->interpret($c) }
@@ -44,7 +46,7 @@ sub new {
else {
# Initialize
- $mt = $self->{_mt_cache}->{$path} = Mojo::Template->new;
+ $mt = $r->{_mt_cache}->{$path} = Mojo::Template->new;
$$output = $mt->render_file($path, $c);
}
@@ -80,6 +82,46 @@ sub new {
return ref $$output ? 0 : 1;
}
);
+
+ # Eplite
+ $self->add_handler(
+ eplite => sub {
+ my ($r, $c, $output) = @_;
+
+ # Template
+ my $template = $c->stash->{template};
+ my $class = delete $c->stash->{eplite_class} || 'main';
+
+ # Path
+ my $path =
+ File::Spec->catfile($c->app->renderer->root, $template);
+
+ # Prepare
+ unless ($r->{_mt_cache}->{$path}) {
+
+ # Data
+ my $d = Mojo::Script->new->get_data($template, $class);
+ unless ($d) {
+
+ # Nothing found
+ $c->app->log->debug(
+ qq/Template "$template" not found in class "$class"./
+ );
+ return;
+ }
+
+ # Template
+ my $t = Mojo::Template->new;
+ $t->parse($d);
+ $t->build;
+ $r->{_mt_cache}->{$path} = $t;
+ }
+
+ # Render
+ return $r->handler->{epl}->($r, $c, $output);
+ }
+ );
+
return $self;
}
View
@@ -5,7 +5,7 @@
use strict;
use warnings;
-use Test::More tests => 93;
+use Test::More tests => 101;
use FindBin;
use lib "$FindBin::Bin/lib";
@@ -217,6 +217,22 @@ is($tx->res->headers->server, 'Mojo (Perl)');
is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
like($tx->res->body, qr/Same old in green Seems to work!/);
+# SingleFileTestApp::Foo::eplite_template
+$tx = Mojo::Transaction->new_get('/foo/eplite_template');
+$client->process_app('SingleFileTestApp', $tx);
+is($tx->res->code, 200);
+is($tx->res->headers->server, 'Mojo (Perl)');
+is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
+is($tx->res->body, "23 works!\n");
+
+# SingleFileTestApp::Foo::eplite_template
+$tx = Mojo::Transaction->new_get('/foo/eplite_template2');
+$client->process_app('SingleFileTestApp', $tx);
+is($tx->res->code, 200);
+is($tx->res->headers->server, 'Mojo (Perl)');
+is($tx->res->headers->header('X-Powered-By'), 'Mojo (Perl)');
+is($tx->res->body, "This one works too!\n");
+
# SingleFileTestApp::Foo::bar
$tx = Mojo::Transaction->new_get('/foo/bar');
$client->process_app('SingleFileTestApp', $tx);
@@ -33,6 +33,25 @@ sub bar {
$self->res->body($self->ctx->url_for);
}
+sub eplite_template {
+ shift->render(
+ template => 'index.html.eplite',
+ eplite_class => 'SingleFileTestApp::Foo'
+ );
+}
+
+sub eplite_template2 {
+ shift->render(
+ template => 'too.html.eplite',
+ eplite_class => 'SingleFileTestApp::Foo'
+ );
+}
+
sub index { shift->stash(template => 'withlayout', msg => 'works great!') }
1;
+__DATA__
+__index.html.eplite__
+<%= 20 + 3 %> works!
+__too.html.eplite__
+This one works too!

0 comments on commit 5c6c8bd

Please sign in to comment.