most of the new pluggable template system

This commit is contained in:
JT Smith 2006-01-17 23:09:29 +00:00
parent ba2774a146
commit eda7058b61
11 changed files with 450 additions and 9 deletions

View file

@ -0,0 +1,77 @@
package WebGUI::Asset::Template::HTMLTemplate;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Asset::Template::Parser';
use HTML::Template;
#-------------------------------------------------------------------
=head2 getName ( )
Returns the human readable name of this parser.
=cut
sub getName {
my $self = shift;
return "HTML::Template";
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template->new(
scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0
);
};
unless ($@) {
$t->param(%{$vars});
return $t->output;
} else {
$self->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($self->session, 'Asset_Template')->get('template error').$@;
}
}
1;

View file

@ -0,0 +1,73 @@
package WebGUI::Asset::Template::HTMLTemplateExpr;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Asset::Template::Parser';
use HTML::Template::Expr;
#-------------------------------------------------------------------
=head2 getName ( )
Returns the human readable name of this parser.
=cut
sub getName {
my $self = shift;
return "HTML::Template::Expr";
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
sub process {
my $class = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my $t;
eval {
$t = HTML::Template::Expr->new(scalarref=>\$template,
global_vars=>1,
loop_context_vars=>1,
die_on_bad_params=>0,
no_includes=>1,
strict=>0);
};
unless ($@) {
$t->param(%{$vars});
return $t->output;
} else {
$self->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($self->session,'Asset_Template')->get('template error').$@;
}
}
1;

View file

@ -0,0 +1,100 @@
package WebGUI::Asset::Template::Parser;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use WebGUI::International;
#-------------------------------------------------------------------
=head2 addSessionVars ( vars )
Appends session variables to the variable list.
=head3 vars
A reference the template variable hash.
=cut
sub addSessionVars {
my $self = shift;
my $vars = shift;
while (my ($section, $hash) = each %{$self->session}) {
next unless (ref $hash eq 'HASH');
while (my ($key, $value) = each %$hash) {
unless (lc($key) eq "password" || lc($key) eq "identifier") {
$vars->{"session.".$section.".".$key} = $value;
}
}
}
$vars->{"webgui.version"} = $WebGUI::VERSION;
$vars->{"webgui.status"} = $WebGUI::STATUS;
return $vars;
}
#-------------------------------------------------------------------
=head2 new ( session )
Constructor.
=head3 session
A reference to the current session.
=cut
sub new {
my $class = shift;
my $session = shift;
bless {_session=>$session}, $class;
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML. This method is required to be overridden.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
sub process { }
#-------------------------------------------------------------------
=head2 session ( )
A reference to the current session.
=cut
sub session {
my $self = shift;
return $self->{_session};
}
1;

View file

@ -0,0 +1,91 @@
package WebGUI::Asset::Template::TemplateToolkit;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Asset::Template::Parser';
use Template;
#-------------------------------------------------------------------
sub _rewriteVars { # replace dots with underscrores in keys (except in keys that aren't usable as variables (URLs etc.))
my $vars = shift;
foreach my $key (keys %$vars){
my $newKey = $key;
$newKey =~ s/\./_/g if $newKey !~ /\//;
if(ref $vars->{$key} eq 'HASH'){
$vars->{$newKey} = _rewriteVars($vars->{$key});
delete $vars->{$key} if($key ne $newKey);
}else{
if($key ne $newKey){
$vars->{$newKey} = $vars->{$key};
delete $vars->{$key};
}
}
}
return $vars;
}
#-------------------------------------------------------------------
=head2 getName ( )
Returns the human readable name of this parser.
=cut
sub getName {
my $self = shift;
return "Template Toolkit";
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
my ($t,$output);
eval {
$t = Template->new( {
INTERPOLATE => 1, # expand "$var" in plain text
POST_CHOMP => 1, # cleanup whitespace
EVAL_PERL => 0, # evaluate Perl code blocks
});
$t->process( \$template, _rewriteVars($vars),\$output) || $self->session->errorHandler->error($t->error());
};
unless($@){
return $output;
} else {
$self->session->errorHandler->error("Error in template. ".$@);
return WebGUI::International->new($self->session,'Asset_Template')->get('template error').$@;
}
}
1;

View file

@ -0,0 +1,59 @@
package WebGUI::Asset::Template::SomeTemplateType;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2006 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Asset::Template::Parser';
#-------------------------------------------------------------------
=head2 getName ( )
Returns the human readable name of this parser.
=cut
sub getName {
my $self = shift;
return "My New Parser";
}
#-------------------------------------------------------------------
=head2 process ( template, vars )
Evaluate a template replacing template commands for HTML.
=head3 template
A scalar variable containing the template.
=head3 vars
A hash reference containing template variables and loops.
=cut
sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
...
}
1;

View file

@ -60,7 +60,7 @@ sub get {
my $self = shift;
my $param = shift;
my $value = $self->{_config}->get($param);
if (isIn($param, qw(sitename assets utilityAssets assetContainers authMethods shippingPlugins paymentPlugins))) {
if (isIn($param, qw(sitename templateParsers assets utilityAssets assetContainers authMethods shippingPlugins paymentPlugins))) {
if (ref $value ne "ARRAY") {
$value = [$value];
}

View file

@ -84,6 +84,19 @@ sub DESTROY {
#-------------------------------------------------------------------
=head2 get ()
An alias for process()
=cut
sub get {
my $self = shift;
$self->process(@_);
}
#-------------------------------------------------------------------
=head2 new ( session )