[Catalyst-commits] r13527 - in Catalyst-View-TT/trunk: .
lib/Catalyst/View t t/lib/TestApp/Controller
t/lib/TestApp/View/TT t/lib/TestApp/root
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Wed Aug 25 09:04:13 GMT 2010
Author: lukes
Date: 2010-08-25 10:04:13 +0100 (Wed, 25 Aug 2010)
New Revision: 13527
Added:
Catalyst-View-TT/trunk/t/12expose_methods.t
Catalyst-View-TT/trunk/t/lib/TestApp/View/TT/ExposeMethods.pm
Catalyst-View-TT/trunk/t/lib/TestApp/root/expose_methods.tt
Modified:
Catalyst-View-TT/trunk/
Catalyst-View-TT/trunk/lib/Catalyst/View/TT.pm
Catalyst-View-TT/trunk/t/lib/TestApp/Controller/Root.pm
Catalyst-View-TT/trunk/t/lib/TestApp/root/test.tt
Log:
r14064 at dev05 (orig r13517): lukes | 2010-08-24 18:29:29 +0200
new branch for exporting view methods to template
r14065 at dev05 (orig r13518): lukes | 2010-08-24 20:08:45 +0200
added expose_methods functionality
r14066 at dev05 (orig r13519): lukes | 2010-08-24 20:19:43 +0200
added all the missing files
r14072 at dev05 (orig r13524): lukes | 2010-08-25 10:56:13 +0200
use accessor for expose_methods rather than access config directly
r14073 at dev05 (orig r13525): lukes | 2010-08-25 11:00:42 +0200
weaken ctx to avoid leakage
r14074 at dev05 (orig r13526): lukes | 2010-08-25 11:03:19 +0200
added me to contribs in case i ever need a job
Property changes on: Catalyst-View-TT/trunk
___________________________________________________________________
Modified: svk:merge
- 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-View-TT/branches/render_die:13040
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-View-TT:10559
+ 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-View-TT/branches/export_methods:13526
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-View-TT/branches/render_die:13040
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-View-TT:10559
Modified: Catalyst-View-TT/trunk/lib/Catalyst/View/TT.pm
===================================================================
--- Catalyst-View-TT/trunk/lib/Catalyst/View/TT.pm 2010-08-25 09:03:19 UTC (rev 13526)
+++ Catalyst-View-TT/trunk/lib/Catalyst/View/TT.pm 2010-08-25 09:04:13 UTC (rev 13527)
@@ -8,11 +8,12 @@
use Template;
use Template::Timer;
use MRO::Compat;
-use Scalar::Util qw/blessed/;
+use Scalar::Util qw/blessed weaken/;
our $VERSION = '0.34';
__PACKAGE__->mk_accessors('template');
+__PACKAGE__->mk_accessors('expose_methods');
__PACKAGE__->mk_accessors('include_path');
*paths = \&include_path;
@@ -42,6 +43,7 @@
PRE_PROCESS => 'config/main',
WRAPPER => 'site/wrapper',
render_die => 1, # Default for new apps, see render method docs
+ expose_methods => [qw/method_in_view_class/],
);
# render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm
@@ -127,6 +129,7 @@
# Set base include paths. Local'd in render if needed
$self->include_path($config->{INCLUDE_PATH});
+ $self->expose_methods($config->{expose_methods});
$self->config($config);
# Creation of template outside of call to new so that we can pass [ $self ]
@@ -264,16 +267,33 @@
return () unless $c;
my $cvar = $self->config->{CATALYST_VAR};
- defined $cvar
+ my %vars = defined $cvar
? ( $cvar => $c )
: (
c => $c,
base => $c->req->base,
name => $c->config->{name}
- )
+ );
+
+ if ($self->expose_methods) {
+ my $meta = $self->meta;
+ foreach my $method_name (@{$self->expose_methods}) {
+ my $method = $meta->get_method( $method_name );
+ unless ($method) {
+ Catalyst::Exception->throw( "$method_name not found in TT view" );
+ }
+ my $method_body = $method->body;
+ my $weak_ctx = $c;
+ weaken $weak_ctx;
+ my $sub = sub {
+ $self->$method_body($weak_ctx, @_);
+ };
+ $vars{$method_name} = $sub;
+ }
+ }
+ return %vars;
}
-
1;
__END__
@@ -558,6 +578,28 @@
The list of paths TT will look for templates in.
+=head2 expose_methods
+
+The list of methods in your View class which should be made available to the templates.
+
+For example:
+
+ expose_methods => [qw/uri_for_static/],
+
+ ...
+
+ sub uri_for_css {
+ my ($self, $c, $filename) = @_;
+
+ # additional complexity like checking file exists here
+
+ return $c->uri_for('/static/css/' . $filename);
+ }
+
+Then in the template:
+
+ [% uri_for_css('home.css') %]
+
=head2 C<CATALYST_VAR>
Allows you to change the name of the Catalyst context object. If set, it will also
@@ -686,6 +728,8 @@
Andy Wardley, C<abw at cpan.org>
+Luke Saunders, C<luke.saunders at gmail.com>
+
=head1 COPYRIGHT
This program is free software. You can redistribute it and/or modify it
Added: Catalyst-View-TT/trunk/t/12expose_methods.t
===================================================================
--- Catalyst-View-TT/trunk/t/12expose_methods.t (rev 0)
+++ Catalyst-View-TT/trunk/t/12expose_methods.t 2010-08-25 09:04:13 UTC (rev 13527)
@@ -0,0 +1,12 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use_ok('Catalyst::Test', 'TestApp');
+
+my $response;
+ok(($response = request("/test?view=ExposeMethods&template=expose_methods.tt"))->is_success, 'request ok');
+is($response->content, "magic added param", 'message ok');
Modified: Catalyst-View-TT/trunk/t/lib/TestApp/Controller/Root.pm
===================================================================
--- Catalyst-View-TT/trunk/t/lib/TestApp/Controller/Root.pm 2010-08-25 09:03:19 UTC (rev 13526)
+++ Catalyst-View-TT/trunk/t/lib/TestApp/Controller/Root.pm 2010-08-25 09:04:13 UTC (rev 13527)
@@ -12,6 +12,7 @@
my ($self, $c) = @_;
$c->stash->{message} = ($c->request->param('message') || $c->config->{default_message});
+ $c->stash->{template} = $c->request->param('template');
}
sub test_includepath : Local {
Added: Catalyst-View-TT/trunk/t/lib/TestApp/View/TT/ExposeMethods.pm
===================================================================
--- Catalyst-View-TT/trunk/t/lib/TestApp/View/TT/ExposeMethods.pm (rev 0)
+++ Catalyst-View-TT/trunk/t/lib/TestApp/View/TT/ExposeMethods.pm 2010-08-25 09:04:13 UTC (rev 13527)
@@ -0,0 +1,20 @@
+package TestApp::View::TT::ExposeMethods;
+
+use Moose;
+extends 'Catalyst::View::TT';
+
+__PACKAGE__->config(
+ expose_methods => [q/exposed_method/],
+);
+
+sub exposed_method {
+ my ($self, $c, $some_param) = @_;
+
+ unless ($some_param) {
+ Catalyst::Exception->throw( "no param passed" );
+ }
+ return 'magic ' . $some_param;
+}
+
+
+1;
Added: Catalyst-View-TT/trunk/t/lib/TestApp/root/expose_methods.tt
===================================================================
--- Catalyst-View-TT/trunk/t/lib/TestApp/root/expose_methods.tt (rev 0)
+++ Catalyst-View-TT/trunk/t/lib/TestApp/root/expose_methods.tt 2010-08-25 09:04:13 UTC (rev 13527)
@@ -0,0 +1 @@
+[% exposed_method('added param') %]
\ No newline at end of file
Modified: Catalyst-View-TT/trunk/t/lib/TestApp/root/test.tt
===================================================================
--- Catalyst-View-TT/trunk/t/lib/TestApp/root/test.tt 2010-08-25 09:03:19 UTC (rev 13526)
+++ Catalyst-View-TT/trunk/t/lib/TestApp/root/test.tt 2010-08-25 09:04:13 UTC (rev 13527)
@@ -1 +1 @@
-[% message %]
+[% message %]
\ No newline at end of file
More information about the Catalyst-commits
mailing list