add Template Toolkit plugins/extentions
This commit is contained in:
parent
1f750691c0
commit
587d494501
2 changed files with 114 additions and 0 deletions
25
lib/WebGUI/Template/Plugin/Macro.pm
Normal file
25
lib/WebGUI/Template/Plugin/Macro.pm
Normal file
|
|
@ -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;
|
||||
|
||||
|
||||
89
lib/WebGUI/Template/Provider.pm
Normal file
89
lib/WebGUI/Template/Provider.pm
Normal file
|
|
@ -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 = <<ENDHTML;
|
||||
[% INCLUDE asset:/asset/url %]
|
||||
[% INSERT template:TEMPLATE_ID %]
|
||||
ENDHTML
|
||||
|
||||
my $provider = WebGUI::Template::Provider->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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue