Snippets can now select a template parser

(instead of being restricted to the configured default)
This commit is contained in:
Paul Driver 2011-03-31 15:19:03 -05:00
parent 3ad6c4eb3a
commit 3531c4d913
8 changed files with 197 additions and 34 deletions

View file

@ -4,6 +4,8 @@
- fixed #12084: Greenportal links are sometimes white on white
- rfe #618: Syndicated Content Asset: Make images in the downloaded RSS-feeds available in the template.
- fixed #12086: Shop Billing Address Unpopulated
- Snippets can now select a template parser (instead of being restricted to
the configured default)
7.10.12
- fixed #12072: Product, related and accessory assets

View file

@ -32,6 +32,7 @@ my $session = start(); # this line required
# upgrade functions go here
addAutoPlayToCarousel( $session );
addProcessorDropdownToSnippet( $session );
finish($session); # this line required
@ -56,6 +57,36 @@ sub addAutoPlayToCarousel {
print "DONE!\n" unless $quiet;
}
#----------------------------------------------------------------------------
sub addProcessorDropdownToSnippet {
my $session = shift;
my $db = $session->db;
print "\tUpdating the Snippet table to add templateProcessor option..."
unless $quiet;
my $rows = $db->buildArrayRefOfHashRefs(q{
select assetId, revisionDate from snippet where processAsTemplate = 1
});
$db->write(q{
alter table snippet
drop column processAsTemplate,
add column templateParser char(255)
});
my $default = $session->config->get('defaultTemplateParser');
for my $row (@$rows) {
$db->write(q{
update snippet
set templateParser = ?
where assetId = ? and revisionDate = ?
}, [ $default, $row->{assetId}, $row->{revisionDate} ]);
}
print "Done!\n";
}
# -------------- DO NOT EDIT BELOW THIS LINE --------------------------------
#----------------------------------------------------------------------------

View file

@ -63,6 +63,7 @@ sub definition {
my $session = shift;
my $definition = shift;
my $i18n = WebGUI::International->new($session,"Asset_Snippet");
my $t18n = WebGUI::International->new($session,'Asset_Template');
my %properties;
tie %properties, 'Tie::IxHash';
%properties = (
@ -94,13 +95,14 @@ sub definition {
label => $i18n->get("cache timeout"),
hoverHelp => $i18n->get("cache timeout help")
},
processAsTemplate=>{
fieldType=>'yesNo',
label=>$i18n->get('process as template'),
hoverHelp=>$i18n->get('process as template description'),
tab=>"properties",
defaultValue=>0
},
templateParser => {
fieldType => 'templateParser',
allowNone => 1,
label => $t18n->get('parser'),
hoverHelp => $t18n->get('parser description'),
tab => 'properties',
defaultValue => '',
},
mimeType=>{
tab=>"properties",
hoverHelp=>$i18n->get('mimeType description'),
@ -312,8 +314,10 @@ sub view {
: $self->get('snippet')
;
$output = $self->getToolbar.$output if ($session->var->isAdminOn && !$calledAsWebMethod);
if ($self->getValue("processAsTemplate")) {
$output = WebGUI::Asset::Template->processRaw($session, $output, $self->get);
if (my $parser = $self->getValue('templateParser')) {
$output = WebGUI::Asset::Template->processRaw(
$session, $output, $self->get, $parser
);
}
WebGUI::Macro::process($session,\$output);
unless ($noCache) {

View file

@ -140,9 +140,8 @@ sub definition {
defaultValue => 1,
},
parser => {
noFormPost => 1,
fieldType => 'selectBox',
defaultValue => [$session->config->get("defaultTemplateParser")],
fieldType => 'templateParser',
defaultValue => $session->config->get('defaultTemplateParser'),
},
namespace => {
fieldType => 'combo',
@ -404,22 +403,12 @@ sub getEditForm {
templatePreview.js
);
if($config->get("templateParsers")){
my @temparray = @{$config->get("templateParsers")};
tie my %parsers, 'Tie::IxHash';
while(my $a = shift @temparray){
$parsers{$a} = $self->getParser($session, $a)->getName();
}
my $value = [$self->getValue("parser")];
$value = \[$config->get("defaultTemplateParser")] if(!$self->getValue("parser"));
$properties->selectBox(
-name=>"parser",
-options=>\%parsers,
-value=>$value,
-label=>$i18n->get('parser'),
-hoverHelp=>$i18n->get('parser description'),
);
}
$properties->templateParser(
name => 'parser',
label => $i18n->get('parser'),
hoverHelp => $i18n->get('parser description'),
value => $self->getValue('parser'),
);
$properties->jsonTable(
name => 'attachmentsJson',

View file

@ -0,0 +1,122 @@
package WebGUI::Form::TemplateParser;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 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::Form::SelectBox';
use WebGUI::International;
use Tie::IxHash;
=head1 NAME
Package WebGUI::Form::TemplateParser
=head1 DESCRIPTION
A dropdown list for selecting a template parser.
=head1 SEE ALSO
This is a subclass of WebGUI::Form::SelectBox.
=head1 METHODS
The following methods are specifically available from this class. Check the superclass for additional methods.
=cut
#-------------------------------------------------------------------
=head2 areOptionsSettable
No, they aren't.
=cut
sub areOptionsSettable { 0 }
#----------------------------------------------------------------------------
=head2 definition ( [ additionalTerms ] )
See the super class for additional details.
=head3 additionalTerms
The following additional parameters have been added via this sub class.
=head3 allowNone
Set to true if "None" is an acceptable option for this dropdown. Defaults to
false.
=head4 defaultValue
Defaults to the default parser selected in the config file
=cut
sub definition {
my ($class, $session, $definition) = @_;
push @{$definition ||= []}, {
allowNone => {
defaultValue => 0,
},
defaultValue => {
defaultValue => $session->config->get('defaultTemplateParser')
}
};
return $class->SUPER::definition($session, $definition);
}
#-------------------------------------------------------------------
=head2 getName ( session )
Returns the human readable name of this control.
=cut
sub getName {
my ($self, $session) = @_;
return WebGUI::International->new($session, 'WebGUI')->get('Template Parser');
}
#-------------------------------------------------------------------
=head2 getOptions
Called by the superclass to determine which options are presented.
=cut
sub getOptions {
my $self = shift;
my $session = $self->session;
tie my %o, 'Tie::IxHash';
if ($self->get('allowNone')) {
$o{''} = WebGUI::International->new($session, 'WebGUI')->get('881');
}
return \%o unless my $parsers = $session->config->get('templateParsers');
for my $class (@$parsers) {
my $parser = WebGUI::Asset::Template->getParser($session, $class);
$o{$class} = $parser->getName;
}
return \%o;
}
1;

View file

@ -520,6 +520,11 @@ our $I18N = {
lastUpdated => 1133087205
},
'Template Parser' => {
message => q|Template Parser|,
lastUpdated => 1301593691
},
'391' => {
message => q|Delete attached file.|,
lastUpdated => 1031514049

View file

@ -33,7 +33,7 @@ isa_ok($snippet, 'WebGUI::Asset::Snippet');
# Test to see if we can set values
my $properties = {
cacheTimeout => 124,
processAsTemplate => 1,
templateParser => 'WebGUI::Asset::Template::HTMLTemplate',
mimeType => 'text/plain',
snippet => "Gooey's milkshake brings all the girls to the yard...",
};
@ -74,9 +74,9 @@ isnt ($editOutput, undef, 'www_edit returns something');
$snippet->update({
title => "authMethod",
processAsTemplate => 1,
templateParser => 'WebGUI::Asset::Template::TemplateToolkit',
cacheTimeout => 1,
snippet => q|^SQL(select value from settings where name="<tmpl_var title>");|
snippet => q|^SQL(select value from settings where name="[% title %]");|
});
WebGUI::Test->originalConfig('macros');

View file

@ -23,6 +23,8 @@ use Test::Exception;
use JSON qw{ from_json };
my $session = WebGUI::Test->session;
my $default = $session->config->get('defaultTemplateParser');
my $ht = 'WebGUI::Asset::Template::HTMLTemplate';
my $list = WebGUI::Asset::Template->getList($session);
cmp_deeply($list, {}, 'getList with no classname returns an empty hashref');
@ -33,16 +35,19 @@ my %var = (
conditional=>1,
loop=>[{},{},{},{},{}]
);
my $output = WebGUI::Asset::Template->processRaw($session,$tmplText,\%var);
my $output = WebGUI::Asset::Template->processRaw($session,$tmplText,\%var, $ht);
ok($output =~ m/\bAAAAA\b/, "processRaw() - variables");
ok($output =~ m/true/, "processRaw() - conditionals");
ok($output =~ m/\s(?:XY){5}\s/, "processRaw() - loops");
my $importNode = WebGUI::Asset::Template->getImportNode($session);
my $template = $importNode->addChild({className=>"WebGUI::Asset::Template", title=>"test", url=>"testingtemplates", template=>$tmplText, namespace=>'WebGUI Test Template'});
my $template = $importNode->addChild({className=>"WebGUI::Asset::Template"});
is($template->get('parser'), $default, "default parser is $default");
$template = $importNode->addChild({className=>"WebGUI::Asset::Template", title=>"test", url=>"testingtemplates", template=>$tmplText, namespace=>'WebGUI Test Template',parser=>$ht});
isa_ok($template, 'WebGUI::Asset::Template', "creating a template");
is($template->get('parser'), 'WebGUI::Asset::Template::HTMLTemplate', 'default parser is HTMLTemplate');
$var{variable} = "BBBBB";
$output = $template->process(\%var);
@ -111,6 +116,7 @@ my $template3 = $importNode->addChild({
title => 'headBlock test',
headBlock => "tag1 tag2 tag3",
template => "this is a template",
parser => $ht,
}, undef, time()-5);
ok(!$template3->get('headBlock'), 'headBlock is empty');
@ -205,6 +211,7 @@ my $trashTemplate = $importNode->addChild({
className => "WebGUI::Asset::Template",
title => 'Trash template',
template => q|Trash Trash Trash Trash|,
parser => $ht,
});
$trashTemplate->trash;
@ -229,6 +236,7 @@ my $brokenTemplate = $importNode->addChild({
className => "WebGUI::Asset::Template",
title => 'Broken template',
template => q|<tmpl_if unclosedIf>If clause with no ending tag|,
parser => $ht,
});
WebGUI::Test->interceptLogging;
@ -251,6 +259,7 @@ my $userStyleTemplate = $importNode->addChild({
url => "ufs",
template => "user function style",
namespace => 'WebGUI Test Template',
parser => $ht,
});
my $someOtherTemplate = $importNode->addChild({
@ -259,6 +268,7 @@ my $someOtherTemplate = $importNode->addChild({
url => "sot",
template => "some other template",
namespace => 'WebGUI Test Template',
parser => $ht,
});
$session->setting->set('userFunctionStyleId', $userStyleTemplate->getId);