Skip to content

Commit

Permalink
Add [wrapper] tag to load a class and instantiate an object with para…
Browse files Browse the repository at this point in the history
…meters passed to the tag.

You can also set a scratch variable to the resulting object and use it elsewhere in tags or ITL.
  • Loading branch information
racke committed Feb 10, 2017
1 parent e19840c commit 2381e04
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions global/wrapper.tag
@@ -0,0 +1,35 @@
UserTag wrapper Order class
UserTag wrapper AddAttr
UserTag wrapper Routine <<EOR
sub {
my ($class, $opt) = @_;
my (%args, $object, $scratch_var);

# copy tag attributes, drop class and reparse
%args = %$opt if $opt;
delete $args{class};
delete $args{reparse};
$scratch_var = delete $args{scratch};

# load class
eval "require $class";

if ($@) {
die "[wrapper]: Failed to load class $class: $@";
}

eval {
$object = $class->new(%args);
};

if ($@) {
die "[wrapper] Failed to instantiate class $class: $@";
}

if ($scratch_var) {
$Scratch->{$scratch_var} = $object;
}

return $object;
}
EOR

0 comments on commit 2381e04

Please sign in to comment.