Todd Rinaldo wrote:
> We have a situation where we needed to render a template where the
> Template object was created with {TOLERANT => 0, EVAL_PERL => 0}.
Hi Todd,
Those are the default values for both those options, and they don't
have any bearing on this issue anyway. So you don't need to worry
about that.
> The test would be to see if fee.fi.fo.fum is defined
[% IF fee.fi.fo.fum.defined %]
> To make things more dificult, the length of the nest isn't always the
> same. For instance, we might need to test if fee.fi.fo.fum.foo.bar is
> defined.
[% IF fee.fi.fo.fum.foo.bar.defined %]
> My question: Is there any easy way to make a recursive block that can
> test if an unpredictably deep hash is defined?
Ah right, so you don't know in advance how many fees, fis, fos and fums you
might have to contend with?
Try this:
[% MACRO is_def(v) GET ${"${v}.defined"} -%]
Use like so:
[% is_def('fee.fi.fo.fum') %]
The macros tacks '.defined' on the end and then calls the stash get() method
to resolve it.
Here's a test script:
use strict;
use warnings;
use Template;
Template->new->process(
\*DATA,
{
a => {
b => {
c => {
true => 1,
false => 0,
undef => undef,
}
}
}
}
);
__DATA__
[% MACRO is_def(v) GET ${"${v}.defined"} -%]
a.b.c.true is [% 'not ' UNLESS a.b.c.true %]true
a.b.c.false is [% 'not ' UNLESS a.b.c.false %]true
a.b.c.undef is [% 'not ' UNLESS a.b.c.undef %]true
a.b.c.missing is [% 'not ' UNLESS a.b.c.missing %]true
a.b.f.g is [% 'not ' UNLESS a.b.f.g %]true
a.h.i.j is [% 'not ' UNLESS a.h.i.j %]true
a.b.c.true.defined is [% 'not ' UNLESS a.b.c.true.defined %]defined
a.b.c.false.defined is [% 'not ' UNLESS a.b.c.false.defined %]defined
a.b.c.undef.defined is [% 'not ' UNLESS a.b.c.undef.defined %]defined
a.b.c.missing.defined is [% 'not ' UNLESS a.b.c.missing.defined %]defined
a.b.f.g.defined is [% 'not ' UNLESS a.b.f.g.defined %]defined
a.h.i.j.defined is [% 'not ' UNLESS a.h.i.j.defined %]defined
is_def('a.b.c.true') is [% 'not ' UNLESS is_def('a.b.c.true') %]defined
is_def('a.b.c.false') is [% 'not ' UNLESS is_def('a.b.c.false') %]defined
is_def('a.b.c.undef') is [% 'not ' UNLESS is_def('a.b.c.undef') %]defined
is_def('a.b.c.missing') is [% 'not ' UNLESS is_def('a.b.c.missing') %]defined
is_def('a.b.f.g') is [% 'not ' UNLESS is_def('a.b.f.g') %]defined
is_def('a.h.i.j') is [% 'not ' UNLESS is_def('a.h.i.j') %]defined
And here's the output:
a.b.c.true is true
a.b.c.false is not true
a.b.c.undef is not true
a.b.c.missing is not true
a.b.f.g is not true
a.h.i.j is not true
a.b.c.true.defined is defined
a.b.c.false.defined is defined
a.b.c.undef.defined is not defined
a.b.c.missing.defined is not defined
a.b.f.g.defined is not defined
a.h.i.j.defined is not defined
is_def('a.b.c.true') is defined
is_def('a.b.c.false') is defined
is_def('a.b.c.undef') is not defined
is_def('a.b.c.missing') is not defined
is_def('a.b.f.g') is not defined
is_def('a.h.i.j') is not defined
How does that look?
A
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates