diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 7490f333a..7c4543419 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -6,6 +6,12 @@ - change: made all LWP user agents use env_proxy - Help: If a Help Chapter only has 1 page, then in the TOC view it links right to the page instead of the Chapter. + - fix: HTML::Template::Expr templates would not handle template variables + with dots in them. Added a fix to the template plugin so that dots are + translated to underscores automatically in submitted template variables. + Templates will still need to be manually updated. + - Help: Added pluggable docs for template plugins, and added a new tab + to the Help that lists template parser docs. 7.1.3 - fix: SQLReport now returns error if can't find DatabaseLink diff --git a/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm b/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm index 2cdfe8684..0d41bab62 100755 --- a/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm +++ b/lib/WebGUI/Asset/Template/HTMLTemplateExpr.pm @@ -19,6 +19,25 @@ use base 'WebGUI::Asset::Template::Parser'; use HTML::Template::Expr; +#------------------------------------------------------------------- +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 ( ) @@ -62,7 +81,7 @@ sub process { strict=>0); }; unless ($@) { - $t->param(%{$vars}); + $t->param(%{_rewriteVars($vars)}); return $t->output; } else { $class->session->errorHandler->error("Error in template. ".$@); diff --git a/lib/WebGUI/Help/Asset_Template.pm b/lib/WebGUI/Help/Asset_Template.pm index 331e8477e..93e85fbde 100644 --- a/lib/WebGUI/Help/Asset_Template.pm +++ b/lib/WebGUI/Help/Asset_Template.pm @@ -149,6 +149,71 @@ our $HELP = { ] }, + 'template parsers list' => { + title => 'template parsers list title', + body => sub { + my $session = shift; + my $dir = join '/', $session->config->getWebguiRoot,"lib","WebGUI","Asset","Template"; + opendir (DIR,$dir) or $session->errorHandler->fatal("Can't open Macro directory: $dir!"); + my @plugins = map { s/\.pm//; $_; } + grep { $_ ne "Parser.pm" } + grep { /\.pm$/ } + readdir(DIR); ##list of namespaces + closedir(DIR); + + ##Build list of enabled macros, by namespace, by reversing session hash: + my @enabledPlugins = map { s/^WebGUI::Asset::Template:://; $_ } @{ $session->config->get("templateParsers") }; + my $defaultParser = $session->config->get('defaultTemplateParser'); + $defaultParser =~ s/^WebGUI::Asset::Template:://; + my %enabledPlugins = map { $_ => 1 } @enabledPlugins; + + my $i18n = WebGUI::International->new($session, 'Asset_Template'); + my $yes = $i18n->get(138, 'WebGUI'); + my $no = $i18n->get(139, 'WebGUI'); + use Data::Dumper; + $session->errorHandler->warn(Dumper \@enabledPlugins); + $session->errorHandler->warn(Dumper \@plugins); + my $plugin_table = + join "\n", + map { join '', '', $_, + '', + ($enabledPlugins{$_} ? $yes : $no), + '', + ($_ eq $defaultParser ? $yes : $no), + '', + } @plugins; + + $plugin_table = + join("\n", + $i18n->get('template parsers list body'), + '', + '',$plugin_table,'
',$i18n->get('plugin name'), + '', + $i18n->get('plugin enabled header'), + '', + $i18n->get('default parser'), + '
'); + }, + fields => [], + related => sub { ##Hey, you gotta pass in the session var, right? + my $session = shift; + sort { $a->{tag} cmp $b->{tag} } + map { + s/^WebGUI::Asset::Template:://; + $tag = $_; + $tag =~ s/^[a-zA-Z]+_//; #Remove initial shortcuts + $tag =~ s/([A-Z]+(?![a-z]))/$1 /g; #Separate acronyms + $tag =~ s/([a-z])([A-Z])/$1 $2/g; #Separate studly caps + $tag =~ s/\s+$//; + $tag = lc $tag; + $namespace = join '', 'Template_', $_; + { tag => $tag, + namespace => $namespace } + } + @{ $session->config->get("templateParsers") } + }, +, + }, }; 1; diff --git a/lib/WebGUI/Help/Macros.pm b/lib/WebGUI/Help/Macros.pm index 3e7d53dbf..63818cf1c 100644 --- a/lib/WebGUI/Help/Macros.pm +++ b/lib/WebGUI/Help/Macros.pm @@ -22,7 +22,7 @@ our $HELP = { my $session = shift; my $dir = join '/', $session->config->getWebguiRoot,"lib","WebGUI","Macro"; opendir (DIR,$dir) or $session->errorHandler->fatal("Can't open Macro directory: $dir!"); - my @macros = map { s/Macro_//; s/\.pm//; $_; } + my @macros = map { s/\.pm//; $_; } grep { /\.pm$/ } readdir(DIR); ##list of namespaces closedir(DIR); diff --git a/lib/WebGUI/Help/Template_HTMLTemplate.pm b/lib/WebGUI/Help/Template_HTMLTemplate.pm new file mode 100644 index 000000000..6b549ef78 --- /dev/null +++ b/lib/WebGUI/Help/Template_HTMLTemplate.pm @@ -0,0 +1,17 @@ +package WebGUI::Help::Template_HTMLTemplate; + +our $HELP = { ##hashref of hashes + 'html template' => { + title => 'html template title', + body => 'html template body', + related => [ + { + tag => 'template language', + namespace => 'Asset_Template', + }, + ], + }, + +}; + +1; ##All perl modules must return true diff --git a/lib/WebGUI/Help/Template_HTMLTemplateExpr.pm b/lib/WebGUI/Help/Template_HTMLTemplateExpr.pm new file mode 100644 index 000000000..97f82ff0d --- /dev/null +++ b/lib/WebGUI/Help/Template_HTMLTemplateExpr.pm @@ -0,0 +1,13 @@ +package WebGUI::Help::Template_HTMLTemplateExpr; + +our $HELP = { ##hashref of hashes + 'html template expr' => { + title => 'html template expr title', + body => 'html template expr body', + related => [ + ], + }, + +}; + +1; ##All perl modules must return true diff --git a/lib/WebGUI/Help/Template_TemplateToolkit.pm b/lib/WebGUI/Help/Template_TemplateToolkit.pm new file mode 100644 index 000000000..9b2f2027a --- /dev/null +++ b/lib/WebGUI/Help/Template_TemplateToolkit.pm @@ -0,0 +1,13 @@ +package WebGUI::Help::Template_TemplateToolkit; + +our $HELP = { ##hashref of hashes + 'template toolkit' => { + title => 'template toolkit title', + body => 'template toolkit body', + related => [ + ], + }, + +}; + +1; ##All perl modules must return true diff --git a/lib/WebGUI/Operation/Help.pm b/lib/WebGUI/Operation/Help.pm index bb1545cb9..63ba65757 100644 --- a/lib/WebGUI/Operation/Help.pm +++ b/lib/WebGUI/Operation/Help.pm @@ -211,6 +211,9 @@ sub _getHelpName { elsif ($file =~ /^Workflow_Activity_/) { $helpName = 'activityName'; } + elsif ($file =~ /^Template_/) { + $helpName = 'templateParserName'; + } else { $helpName = 'topicName'; } @@ -435,6 +438,10 @@ sub www_viewHelpTOC { label => $i18n->get('topicName', 'Workflow'), uiLevel => 1, }, + templ => { + label => $i18n->get('template parsers', 'Asset_Template'), + uiLevel => 1, + }, ); my @files = _getHelpFilesList($session,); @@ -449,8 +456,6 @@ sub www_viewHelpTOC { my $helpTopic = _loadHelp($session, "WebGUI::Help::".$moduleName); my @helpEntries = keys %{ $helpTopic }; my $link; - $session->errorHandler->warn("MOD: ". $moduleName. " ".scalar(@helpEntries)); - $session->errorHandler->warn("MOD: ". $moduleName); if (scalar @helpEntries > 1) { ##Chapter $link = _linkTOC($session, $moduleName); diff --git a/lib/WebGUI/i18n/English/Asset_Template.pm b/lib/WebGUI/i18n/English/Asset_Template.pm index 415a39632..6cd502224 100644 --- a/lib/WebGUI/i18n/English/Asset_Template.pm +++ b/lib/WebGUI/i18n/English/Asset_Template.pm @@ -439,6 +439,36 @@ be on the right side of the page.

lastUpdated => 1146456174, }, + 'plugin name' => { + message => q|Parser Name|, + lastUpdated => 1162087997, + }, + + 'plugin enabled header' => { + message => q|Enabled?|, + lastUpdated => 1162088018, + }, + + 'template parsers' => { + message => q|Template Parsers|, + lastUpdated => 1162088018, + }, + + 'default parser' => { + message => q|Default Parser|, + lastUpdated => 1162088018, + }, + + 'template parsers list title' => { + message => q|List of Template Parsers|, + lastUpdated => 1162088018, + }, + + 'template parsers list body' => { + message => q|

The following template parsers are installed on your site and may be enabled for use.

|, + lastUpdated => 1162088018, + }, + }; 1; diff --git a/lib/WebGUI/i18n/English/Template_HTMLTemplate.pm b/lib/WebGUI/i18n/English/Template_HTMLTemplate.pm new file mode 100644 index 000000000..332df9f72 --- /dev/null +++ b/lib/WebGUI/i18n/English/Template_HTMLTemplate.pm @@ -0,0 +1,23 @@ +package WebGUI::i18n::English::Template_HTMLTemplate; + +our $I18N = { + 'html template title' => { + message => q|HTML Template|, + lastUpdated => 1162085513, + context => q|Name of the HTML::Template module| + }, + + 'html template body' => { + message => q|

HTML Template is the default template parser inside of WebGUI. All of the template documentation in the online help is written using HTML Template syntax.

|, + lastUpdated => 1162085513, + context => q|Explanation of HTML Template usage in WebGUI.| + }, + + 'templateParserName' => { + message => q|HTML Template|, + lastUpdated => 1162085447, + }, + +}; + +1; diff --git a/lib/WebGUI/i18n/English/Template_HTMLTemplateExpr.pm b/lib/WebGUI/i18n/English/Template_HTMLTemplateExpr.pm new file mode 100644 index 000000000..33da2e1bc --- /dev/null +++ b/lib/WebGUI/i18n/English/Template_HTMLTemplateExpr.pm @@ -0,0 +1,25 @@ +package WebGUI::i18n::English::Template_HTMLTemplateExpr; + +our $I18N = { + 'html template expr title' => { + message => q|HTML Template Expr|, + lastUpdated => 1162085513, + context => q|Name of the HTML::Template::Expr module| + }, + + 'html template expr body' => { + message => q|

HTML Template Expr is an extension to HTML Template that adds expressions to the language. Documentation for this extension is available online.

+

The syntax of template variables in HTML Template Expr is slightly different from HTML Template. Any WebGUI template variables that contains a dot '.', should be chanaged to use an underscore instead '_'.

+|, + lastUpdated => 1162085513, + context => q|Explanation of HTML Template Expr usage in WebGUI.| + }, + + 'templateParserName' => { + message => q|HTML Template with expressions|, + lastUpdated => 1162085447, + }, + +}; + +1; diff --git a/lib/WebGUI/i18n/English/Template_TemplateToolkit.pm b/lib/WebGUI/i18n/English/Template_TemplateToolkit.pm new file mode 100644 index 000000000..8e8398971 --- /dev/null +++ b/lib/WebGUI/i18n/English/Template_TemplateToolkit.pm @@ -0,0 +1,25 @@ +package WebGUI::i18n::English::Template_TemplateToolkit; + +our $I18N = { + 'template toolkit title' => { + message => q|Template Toolkit|, + lastUpdated => 1162085513, + context => q|Name of the Template::Toolkit module| + }, + + 'template toolkit body' => { + message => q|

Template Toolkit is is a collection of modules which implement a fast, flexible, powerful and extensible template processing system. Documentation for Template Toolkit is available online.

+

The syntax of template variables in Template Toolkit is slightly different from HTML Template. Any WebGUI template variables that contains a dot '.', should be chanaged to use an underscore instead '_'.

+|, + lastUpdated => 1162085513, + context => q|Explanation of Template Toolkit usage in WebGUI.| + }, + + 'templateParserName' => { + message => q|Template Toolkit|, + lastUpdated => 1162086264, + }, + +}; + +1; diff --git a/lib/WebGUI/i18n/English/_i18n.skeleton b/lib/WebGUI/i18n/English/_i18n.skeleton index e7cff4860..d07c1b106 100644 --- a/lib/WebGUI/i18n/English/_i18n.skeleton +++ b/lib/WebGUI/i18n/English/_i18n.skeleton @@ -16,6 +16,7 @@ our $I18N = { ##hashref of hashes #If the help file documents an Asset, it must include an assetName key #If the help file documents an Macro, it must include an macroName key #If the help file documents a Workflow Activity, it must include an activityName key + #If the help file documents a Template Parser, it must include an templateParserName key #For all other types, use topicName 'assetName' => { message => q|My Asset|, diff --git a/t/Asset/Template/HTMLTemplateExpr.t b/t/Asset/Template/HTMLTemplateExpr.t new file mode 100644 index 000000000..c2616fe3e --- /dev/null +++ b/t/Asset/Template/HTMLTemplateExpr.t @@ -0,0 +1,73 @@ +#------------------------------------------------------------------- +# 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 +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use HTML::TokeParser; + +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Asset; +use WebGUI::VersionTag; +use WebGUI; + +use Test::More; + +my $num_tests = 3; +plan tests => 2 + $num_tests; + +my $session = WebGUI::Test->session; + +# put your tests here + +my $module = use_ok('HTML::Template::Expr', 'Loaded HTML::Template::Expr'); +my $plugin = use_ok('WebGUI::Asset::Template::HTMLTemplateExpr', 'Loaded WebGUI::Asset::Template::HTMLTemplateExpr plugin'); + +my ($versionTag, $template); +my $originalParsers = $session->config->get('templateParsers'); + +SKIP: { + skip $num_tests, "HTML::Template::Expr or plugin not loaded" unless $module and $plugin; + + $session->config->set('templateParsers', ['WebGUI::Asset::Template::HTMLTemplate', 'WebGUI::Asset::Template::HTMLTemplateExpr',] ); + ($versionTag, $template) = setup_assets($session); + my $templateOutput = $template->process({ "foo.bar" => "baz", "number.value" => 2 }); + my $companyName = $session->config->get('companyName'); + like($templateOutput, qr/NAME=$companyName/, "session variable with underscores"); + like($templateOutput, qr/FOOBAR=baz/, "explicit variable with dots"); + like($templateOutput, qr/EQN=4/, "explicit variable with dots in expr"); +} + +sub setup_assets { + my $session = shift; + my $importNode = WebGUI::Asset->getImportNode($session); + my $versionTag = WebGUI::VersionTag->getWorking($session); + $versionTag->set({name=>"HTMLTemplateExpr test"}); + my $properties = { + title => 'HTML Template Expr test', + className => 'WebGUI::Asset::Template', + url => 'dotted', + parser => 'WebGUI::Asset::Template::HTMLTemplateExpr', + id => 'htmltemplateexpr000001', + # '1234567890123456789012' + template => q!NAME=\nFOOBAR=\nEQN=!, + }; + my $template = $importNode->addChild($properties, $properties->{id}); + $versionTag->commit; + return ($versionTag, $template); +} + +END { + $session->config->set('templateParsers', $originalParsers); + if (defined $versionTag and ref $versionTag eq 'WebGUI::VersionTag') { + $versionTag->rollback; + } +}