diff options
author | mascip (Pierre Masci) <piemas25+git@gmail.com> | 2013-12-09 13:22:21 +0000 |
---|---|---|
committer | Pierre Masci <mascip@gmail.com> | 2014-03-19 14:19:16 +0000 |
commit | bb3ff3e1f924d530b3bfdf192849b5ac1b9aa5b2 (patch) | |
tree | e5dc5c2c861027aaccae415f4c9aa5bea896bcc3 /lib | |
parent | 1f06a7f7efbef91939335b070c279ede002af32f (diff) | |
download | ox-view-tt-bb3ff3e1f924d530b3bfdf192849b5ac1b9aa5b2.tar.gz ox-view-tt-bb3ff3e1f924d530b3bfdf192849b5ac1b9aa5b2.zip |
template_params dependency, to send parameters to the template
Diffstat (limited to 'lib')
-rw-r--r-- | lib/OX/View/TT.pm | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/OX/View/TT.pm b/lib/OX/View/TT.pm index 5e11579..9f8697e 100644 --- a/lib/OX/View/TT.pm +++ b/lib/OX/View/TT.pm @@ -10,17 +10,28 @@ use Template; package MyApp; use OX; + has 'template_params' => ( + block => sub { + my $s = shift; + return { + some_scalar => 'value', + some_array => ['one', 'two'], + }; + }, + ); + + + has view => ( is => 'ro', isa => 'OX::View::TT', - dependencies => ['template_root'], + dependencies => ['template_root', 'template_params'], ); =head1 DESCRIPTION This is a very thin wrapper around L<Template> which exposes some OX -functionality to your template files. Templates rendered with this class will -have access to these additional variables: +functionality to your template files. It can be passed a template_params dependency, containing variables that will be passed to the template. Templates rendered with this class will have access to these additional variables: =over 4 @@ -69,9 +80,16 @@ has 'tt' => ( } ); -sub _build_template_params { +has template_params => ( + is => 'ro', + isa => 'HashRef', + default => sub { {} }, +); + +sub _get_all_template_params { my ($self, $r, $params) = @_; return +{ + %{ $self->template_params }, base => $r->script_name, uri_for => sub { $r->uri_for(@_) }, m => $r->mapping, @@ -92,7 +110,7 @@ sub render { my $out = ''; $self->tt->process( $template, - $self->_build_template_params( $r, $params ), + $self->_get_all_template_params( $r, $params ), \$out ) || confess $self->tt->error; $out; @@ -116,7 +134,7 @@ sub template { confess("Must supply a 'template' parameter") unless exists $params{template}; - return $self->render($r, $params{template}); + return $self->render($r, $params{template}, \%params); } __PACKAGE__->meta->make_immutable; |