[Catalyst-commits] r13518 - in
Catalyst-View-TT/branches/export_methods: lib/Catalyst/View
t/lib/TestApp/Controller t/lib/TestApp/root
lukes at dev.catalyst.perl.org
lukes at dev.catalyst.perl.org
Tue Aug 24 18:08:45 GMT 2010
Author: lukes
Date: 2010-08-24 19:08:45 +0100 (Tue, 24 Aug 2010)
New Revision: 13518
Modified:
Catalyst-View-TT/branches/export_methods/lib/Catalyst/View/TT.pm
Catalyst-View-TT/branches/export_methods/t/lib/TestApp/Controller/Root.pm
Catalyst-View-TT/branches/export_methods/t/lib/TestApp/root/test.tt
Log:
added expose_methods functionality
Modified: Catalyst-View-TT/branches/export_methods/lib/Catalyst/View/TT.pm
===================================================================
--- Catalyst-View-TT/branches/export_methods/lib/Catalyst/View/TT.pm 2010-08-24 16:29:29 UTC (rev 13517)
+++ Catalyst-View-TT/branches/export_methods/lib/Catalyst/View/TT.pm 2010-08-24 18:08:45 UTC (rev 13518)
@@ -42,6 +42,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
@@ -264,16 +265,31 @@
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->config->{expose_methods}) {
+ my $meta = $self->meta;
+ foreach my $method_name (@{$self->config->{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 $sub = sub {
+ $self->$method_body($c, @_);
+ };
+ $vars{$method_name} = $sub;
+ }
+ }
+ return %vars;
}
-
1;
__END__
@@ -558,6 +574,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
Modified: Catalyst-View-TT/branches/export_methods/t/lib/TestApp/Controller/Root.pm
===================================================================
--- Catalyst-View-TT/branches/export_methods/t/lib/TestApp/Controller/Root.pm 2010-08-24 16:29:29 UTC (rev 13517)
+++ Catalyst-View-TT/branches/export_methods/t/lib/TestApp/Controller/Root.pm 2010-08-24 18:08:45 UTC (rev 13518)
@@ -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 {
Modified: Catalyst-View-TT/branches/export_methods/t/lib/TestApp/root/test.tt
===================================================================
--- Catalyst-View-TT/branches/export_methods/t/lib/TestApp/root/test.tt 2010-08-24 16:29:29 UTC (rev 13517)
+++ Catalyst-View-TT/branches/export_methods/t/lib/TestApp/root/test.tt 2010-08-24 18:08:45 UTC (rev 13518)
@@ -1 +1 @@
-[% message %]
+[% message %]
\ No newline at end of file
More information about the Catalyst-commits
mailing list