From 587d4945018e673778e4327d63d2bb3b51b73e19 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Tue, 17 May 2011 15:20:17 -0500 Subject: [PATCH] add Template Toolkit plugins/extentions --- lib/WebGUI/Template/Plugin/Macro.pm | 25 ++++++++ lib/WebGUI/Template/Provider.pm | 89 +++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 lib/WebGUI/Template/Plugin/Macro.pm create mode 100644 lib/WebGUI/Template/Provider.pm diff --git a/lib/WebGUI/Template/Plugin/Macro.pm b/lib/WebGUI/Template/Plugin/Macro.pm new file mode 100644 index 000000000..dd530b36b --- /dev/null +++ b/lib/WebGUI/Template/Plugin/Macro.pm @@ -0,0 +1,25 @@ +package WebGUI::Template::Plugin::Macro; + +use base 'Template::Plugin'; + +sub new { + my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; + my ($class, $context) = @_; + + my $session = $context->stash->{_session}; + + my $subs = {}; + my $macros = $session->config->get("macros"); + for my $macro ( keys %$macros ) { + my $package = "WebGUI::Macro::\u$macros->{macro}"; + my $process = $package->can('process'); + $subs->{$macro} = sub { + $process->($session, @_); + }; + } + return $subs; +} + +1; + + diff --git a/lib/WebGUI/Template/Provider.pm b/lib/WebGUI/Template/Provider.pm new file mode 100644 index 000000000..366b90d03 --- /dev/null +++ b/lib/WebGUI/Template/Provider.pm @@ -0,0 +1,89 @@ +package WebGUI::Template::Provider; + +use strict; +use base 'Template::Provider'; + +use WebGUI::Asset; +use Try::Tiny; + +=head1 NAME + +WebGUI::Template::Provider - Allow WebGUI assets inside Templates + +=head1 SYNOPSIS + + use Template; + use WebGUI::Template::Provider; + + my $template = <new( $session ); + my $t = Template->new( LOAD_TEMPLATES => [ $provider ] ); + $t->process( $template, $vars ); + +=cut + +sub new { + my ( $class, $session, $options ) = @_; + my $self = $class->SUPER::new( $options ); + $self->session( $session ); + return $self; +} + +sub session { + my ( $self, $newSession ) = @_; + if ( $newSession ) { + $self->{_session} = $newSession; + } + return $self->{_session}; +} + +sub _template_modified { + my ( $self, $path ) = @_; + if ( $path =~ /^(?:asset|template):(\S+)/ ) { + my $id = $1; + my $asset = $self->getAsset( $id ); + return $asset->getLastModified; + } + else { + return $self->SUPER::_template_modified( @_[1..$#_] ); + } +} + +sub _template_content { + my ( $self, $path ) = @_; + if ( $path =~ /^(asset|template):(\S+)/ ) { + my $type = $1; + my $id = $2; + my $asset = eval { $self->getAsset( $id ) }; + if ( $@ ) { + return wantarray ? ( "", $@, 0 ) : ""; + } + + my $content = $type eq 'template' ? $asset->template : $asset->view; + return wantarray ? ( $content, "", $asset->getLastModified ) : $content; + } + else { + return $self->SUPER::_template_content( @_[1..$#_] ); + } +} + +sub getAsset { + my ( $self, $id ) = @_; + try { + return WebGUI::Asset->newByUrl( $self->session, $id ); + } + catch { + try { + return WebGUI::Asset->newById( $self->session, $id ); + } + catch { + die "Could not find asset $id to include in template: " . $_; + }; + }; +} + +1;