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

@ -1,8 +1,9 @@
=======
6.9.0
- Converted WebGUI to use a new object oriented session system. More details
in migation.txt.
- Added a lot more tests to the test suite.
- Added a new pluggable templating system. (Thanks to Misja Op de Coul /
E-Wise)
6.8.6
-

View file

@ -13,6 +13,7 @@ Contributing Developers..............Peter Beardsley / Appropriate Solutions
Irving Carrion
Richard Clark
Doug Collinge
Misja Op de Coul / E-Wise
Flavio Curti
Michele Dell'Aquila / CSU
Jeff Depons / Adaptive Dynamics

View file

@ -12,7 +12,7 @@ use lib "../../lib";
use strict;
use Getopt::Long;
use WebGUI::Session;
use File::Path;
my $toVersion = "6.9.0"; # make this match what version you're going to
my $quiet; # this line required
@ -20,16 +20,33 @@ my $quiet; # this line required
my $session = start(); # this line required
# upgrade functions go here
templateParsers();
removeFiles();
finish($session); # this line required
##-------------------------------------------------
#sub exampleFunction {
# print "\tWe're doing some stuff here that you should know about.\n" unless ($quiet);
# # and here's our code
#}
#-------------------------------------------------
sub templateParsers {
print "\tAdding support for multiple template parsers.\n" unless ($quiet);
$session->db->write("alter table template add column parser varchar(255) not null default 'WebGUI::Asset::Template::HTMLTemplate'");
}
#-------------------------------------------------
sub removeFiles {
print "\tRemoving old unneeded files.\n" unless ($quiet);
unlink '../../lib/WebGUI/ErrorHandler.pm';
unlink '../../lib/WebGUI/HTTP.pm';
unlink '../../lib/WebGUI/Privilege.pm';
unlink '../../lib/WebGUI/DateTime.pm';
unlink '../../lib/WebGUI/FormProcessor.pm';
unlink '../../lib/WebGUI/URL.pm';
unlink '../../lib/WebGUI/Id.pm';
unlink '../../lib/WebGUI/Icon.pm';
unlink '../../lib/WebGUI/Style.pm';
unlink '../../lib/WebGUI/Setting.pm';
unlink '../../lib/WebGUI/Grouping.pm';
}

View file

@ -109,6 +109,15 @@ paymentPlugins = ITransact
shippingPlugins = ByPrice, ByWeight, PerTransaction
# Specify the list of template parsers available in the system.
templateParsers = WebGUI::Asset::Template::HTMLTemplate
# Specify the default template parser.
defaultTemplateParser = WebGUI::Asset::Template::HTMLTemplate
# Specify a the list of assets you want to appear in your
# Add Content menus.

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 )