From eda7058b61b7f4a3a9fada6833c6710776389438 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Tue, 17 Jan 2006 23:09:29 +0000 Subject: [PATCH] most of the new pluggable template system --- docs/changelog/6.x.x.txt | 3 +- docs/credits.txt | 1 + docs/upgrades/upgrade_6.8.5-6.9.0.pl | 31 ++++-- etc/WebGUI.conf.original | 9 ++ lib/WebGUI/Asset/Template/HTMLTemplate.pm | 77 ++++++++++++++ lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm | 73 +++++++++++++ lib/WebGUI/Asset/Template/Parser.pm | 100 ++++++++++++++++++ lib/WebGUI/Asset/Template/TemplateToolkit.pm | 91 ++++++++++++++++ lib/WebGUI/Asset/Template/_parser.skeleton | 59 +++++++++++ lib/WebGUI/Config.pm | 2 +- lib/WebGUI/Session/Form.pm | 13 +++ 11 files changed, 450 insertions(+), 9 deletions(-) create mode 100755 lib/WebGUI/Asset/Template/HTMLTemplate.pm create mode 100755 lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm create mode 100755 lib/WebGUI/Asset/Template/Parser.pm create mode 100755 lib/WebGUI/Asset/Template/TemplateToolkit.pm create mode 100755 lib/WebGUI/Asset/Template/_parser.skeleton diff --git a/docs/changelog/6.x.x.txt b/docs/changelog/6.x.x.txt index f182a67c5..23a62a021 100644 --- a/docs/changelog/6.x.x.txt +++ b/docs/changelog/6.x.x.txt @@ -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 - diff --git a/docs/credits.txt b/docs/credits.txt index cb8fd373c..28270f055 100644 --- a/docs/credits.txt +++ b/docs/credits.txt @@ -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 diff --git a/docs/upgrades/upgrade_6.8.5-6.9.0.pl b/docs/upgrades/upgrade_6.8.5-6.9.0.pl index f0183f95c..5ef8f18af 100644 --- a/docs/upgrades/upgrade_6.8.5-6.9.0.pl +++ b/docs/upgrades/upgrade_6.8.5-6.9.0.pl @@ -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'; +} diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index 52be144c4..a5c8646c3 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -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. diff --git a/lib/WebGUI/Asset/Template/HTMLTemplate.pm b/lib/WebGUI/Asset/Template/HTMLTemplate.pm new file mode 100755 index 000000000..a460457a1 --- /dev/null +++ b/lib/WebGUI/Asset/Template/HTMLTemplate.pm @@ -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; diff --git a/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm b/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm new file mode 100755 index 000000000..6c8e197e4 --- /dev/null +++ b/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm @@ -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; diff --git a/lib/WebGUI/Asset/Template/Parser.pm b/lib/WebGUI/Asset/Template/Parser.pm new file mode 100755 index 000000000..f6fb6730e --- /dev/null +++ b/lib/WebGUI/Asset/Template/Parser.pm @@ -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; diff --git a/lib/WebGUI/Asset/Template/TemplateToolkit.pm b/lib/WebGUI/Asset/Template/TemplateToolkit.pm new file mode 100755 index 000000000..f523e4206 --- /dev/null +++ b/lib/WebGUI/Asset/Template/TemplateToolkit.pm @@ -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; diff --git a/lib/WebGUI/Asset/Template/_parser.skeleton b/lib/WebGUI/Asset/Template/_parser.skeleton new file mode 100755 index 000000000..bb7da41d8 --- /dev/null +++ b/lib/WebGUI/Asset/Template/_parser.skeleton @@ -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; diff --git a/lib/WebGUI/Config.pm b/lib/WebGUI/Config.pm index d2f7f21ca..80baf9592 100644 --- a/lib/WebGUI/Config.pm +++ b/lib/WebGUI/Config.pm @@ -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]; } diff --git a/lib/WebGUI/Session/Form.pm b/lib/WebGUI/Session/Form.pm index d87879a11..689879f8c 100644 --- a/lib/WebGUI/Session/Form.pm +++ b/lib/WebGUI/Session/Form.pm @@ -84,6 +84,19 @@ sub DESTROY { +#------------------------------------------------------------------- + +=head2 get () + +An alias for process() + +=cut + +sub get { + my $self = shift; + $self->process(@_); +} + #------------------------------------------------------------------- =head2 new ( session )