variable downgrading

This commit is contained in:
Paul Driver 2010-09-01 14:39:03 -05:00
parent 5765739a95
commit c89da43b58
5 changed files with 151 additions and 0 deletions

View file

@ -54,6 +54,7 @@ sub process {
my $self = shift;
my $template = shift;
my $vars = $self->addSessionVars(shift);
$self->downgrade($vars);
my $t;
eval {
$t = HTML::Template->new(

View file

@ -81,6 +81,7 @@ sub process {
my $class = shift;
my $template = shift;
my $vars = $class->addSessionVars(shift);
$class->downgrade($vars);
my $t;
eval {
$t = HTML::Template::Expr->new(scalarref=>\$template,

View file

@ -16,6 +16,7 @@ package WebGUI::Asset::Template::Parser;
use strict;
use WebGUI::International;
use Scalar::Util qw(blessed);
#-------------------------------------------------------------------
@ -63,6 +64,49 @@ sub addSessionVars {
#-------------------------------------------------------------------
=head2 downgrade ( vars )
Removes or converts things HTML::Template-like engines can't handle. Coderefs
are removed, blessed objects are removed, and hashes are recursively flattened
by appending keys separated by dots (e.g. { foo => { bar => 'baz' } } becomes
{ 'foo.bar' => 'baz' }. Also, array elements that aren't hashes are converted
to hashes via { value => $bareValue }.
=cut
sub downgrade {
my ($self, $vars) = @_;
for my $k (keys %$vars) {
my $v = $vars->{$k};
if (blessed($v) || ref $v eq 'CODE') {
delete $vars->{$k};
}
elsif (ref $v eq 'ARRAY') {
for my $i (0..$#$v) {
if (ref $v->[$i] eq 'HASH') {
$self->downgrade($v->[$i]);
}
else {
my %hash = ( value => $v->[$i] );
$self->downgrade(\%hash);
$v->[$i] = \%hash;
}
}
}
elsif (ref $v eq 'HASH') {
delete $vars->{$k};
my %flatter;
for my $subkey (keys %$v) {
$flatter{"$k.$subkey"} = $v->{$subkey};
}
$self->downgrade(\%flatter);
@{$vars}{keys %flatter} = values %flatter;
}
}
}
#-------------------------------------------------------------------
=head2 new ( session )
Constructor.