Skip to content

Commit

Permalink
convert to use ::Templated
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Aug 18, 2007
1 parent 634c594 commit be5650a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
5 changes: 3 additions & 2 deletions Makefile.PL
Expand Up @@ -2,11 +2,12 @@ use inc::Module::Install;

name 'Catalyst-View-Template-Declare';
all_from 'lib/Catalyst/View/Template/Declare.pm';
requires 'Catalyst::View';
requires 'Catalyst::View::Templated';
requires 'Template::Declare' => '0.21';
requires 'NEXT';
requires 'Class::C3';
requires 'Module::Pluggable::Object';

build_requires 'Test::More';
build_requires 'Catalyst::Runtime' => '5.7000';
auto_install;
WriteAll;
65 changes: 30 additions & 35 deletions lib/Catalyst/View/Template/Declare.pm
Expand Up @@ -3,12 +3,11 @@ use strict;
my $context;

package Catalyst::View::Template::Declare;
use base qw(Catalyst::View Template::Declare);
use NEXT;
use Template::Declare::Tags;
use base qw(Catalyst::View::Templated);
use Class::C3;
require Module::Pluggable::Object;

our $VERSION = '0.01';
our $VERSION = '0.02';

sub COMPONENT {
my $self = shift;
Expand All @@ -25,7 +24,7 @@ sub COMPONENT {
foreach my $extra (@extras) {
$c->log->info("Loading subtemplate $extra");

# load module (warn on error)
# load module
if (!eval "require $extra"){
die "Couldn't include $extra: $@";
}
Expand All @@ -42,31 +41,18 @@ sub COMPONENT {
Template::Declare->init(roots => [$class, @extras]);

# init superclasses
$self->NEXT::COMPONENT(@_);
$self->next::method($c, @_);
}

sub render {
my ($self, $c, $template, @args) = @_;
$context = $c;
my $out;
eval {
Template::Declare->new_buffer_frame;
$out = Template::Declare->show($template);
Template::Declare->end_buffer_frame;
};
if ($@) {
$c->error($@);
}
return $out;
}
sub _render {
my ($self, $template, $stash, $args) = @_;
$context = $self->context;

sub process {
my ($self, $c, @args) = @_;

my $template = $c->stash->{template} || $c->action;
my $html = $self->render($c, $template, @args);

$c->response->body($html);
Template::Declare->new_buffer_frame;
my $out = Template::Declare->show($template);
Template::Declare->end_buffer_frame;

return $out;
}

package c;
Expand All @@ -86,7 +72,7 @@ Catalyst::View::Template::Declare - Use Template::Declare with Catalyst
=head1 VERSION
Version 0.01
Version 0.02
=head1 SYNOPSIS
Expand Down Expand Up @@ -121,6 +107,11 @@ Make a view:
package MyApp::View::TD;
use base 'Catalyst::View::Template::Declare';
1;
Make a template:
package MyApp::View::TD::Main;
use Template::Declare::Tags;
template foo => sub {
Expand All @@ -132,27 +123,27 @@ Make a view:
In your app:
$c->stash(template => 'foo');
$c->view('TD')->template('foo');
$c->stash(title => 'test');
$c->detach('View::TD');
And get the output:
<html><head><title>test</title></head><body>Hello, world</body></html>
You can also spread your templates out over multiple files. If your
You can spread your templates out over multiple files. If your
view is called MyApp::View::TD, then everything in MyApp::View::TD::*
will be included and templates declared in those files will be available
as though they were declared in your main view class.
as though they were declared in your main view class.
Example:
package MyApp::View::TD::Foo;
use Template::Declare::Tags;
template foo => sub { ... };
template bar => sub { ... };
1;
Then you can set C<< $c->stash(template => 'foo') >> and everything
Then you can set C<< $c->view('TD')->template('bar') >> and everything
will work as you expect.
=head1 METHODS
Expand All @@ -161,9 +152,9 @@ will work as you expect.
Render the template specified by the action or C<< $c->stash->template >>.
=head2 render($c, $template)
=head2 render($template)
Render the template named by C<$template> and return the text.
Render the template named by C<$template> and return the output.
=head2 COMPONENT
Expand All @@ -185,6 +176,10 @@ your bug as I make changes.
Visit #catalyst on irc.perl.org, submit an RT ticket, or send me an e-mail.
=head1 SEE ALSO
L<Catalyst::View::Templated>
=head1 ACKNOWLEDGEMENTS
L<Template::Declare>
Expand Down
8 changes: 0 additions & 8 deletions t/lib/TestApp/View/TD.pm
Expand Up @@ -2,13 +2,5 @@ package TestApp::View::TD;
use strict;
use warnings;
use base 'Catalyst::View::Template::Declare';
use Template::Declare::Tags;
template action_name => sub {
p { "This is the action_name template." };
};

template stash => sub {
p { "Hello, ". c->stash->{world} };
};

1;
16 changes: 16 additions & 0 deletions t/lib/TestApp/View/TD/Main.pm
@@ -0,0 +1,16 @@
# Copyright (c) 2007 Jonathan Rockway <jrockway@cpan.org>

package TestApp::View::TD::Main;
use strict;
use warnings;
use Template::Declare::Tags;

template action_name => sub {
p { "This is the action_name template." };
};

template stash => sub {
p { "Hello, ". c->stash->{world} };
};

1;

0 comments on commit be5650a

Please sign in to comment.