On Wed, Apr 6, 2011 at 5:28 PM, Michael Peters <[email protected]> wrote:
> On 04/06/2011 05:13 PM, Brad Baxter wrote:
>
>> But finally I decided I needed to abandon it,
>> because mentally I just couldn't handle the fact that setting an
>> attribute in the object was dependent on the attribute existing in
>> the template.
>
> I'm not sure I understand this problem. HTML template doesn't really work
> with objects, just hash references in loops. Are are you saying it was using
> your hash-based objects as if they were just hashes? That itself was
> probably not the best idea.
>
> Also, was this just a matter of turning die_on_bad_params off? I know it's
> the default, but if you don't want everything in your hashes to be in the
> template, then that's the right solution.
At the end of this note is code that produces this output:
0:[
[] trees
[rotten] logs
]
1:[
[maple] trees
[rotten] logs
]
2:[[maple] trees]
Note that "maple" appears in 1: but not in 0:, even though
the template object in both cases has these values set:
$tmpl->param( trees => [{ type => 'maple' }] );
$tmpl->param( forest => [{ logs => 'rotten' }] );
This is in fact documented:
NOTE: global_vars is not global_loops (which does not exist). That
means that loops you declare at one scope are not available inside
other loops even when global_vars is on.
So my complaint is that my perl code is setting the
same values in the $tmpl object, but the module
does not "honor" the value unless it sees the loop
in the template a certain way. My explanation is
confusing, I know -- the whole situation is confusing
(to me). And that's what made me have to say
enough finally after running into this multiple times.
You could say I want global everything, but what I
really want is for my template object to actually
contain the values that I set in it, so I can pass it
with "associate" to a child object. I see someone
else is suggesting parent/child relationships be
supported, which is what I essentially was after.
>> __break__
>
> I looked at the docs for HTML::Template::Compiled (which are really sparse
> by the way with lots of circular references) and I'm not quite sure what it
> does.
It lets you output something on every
nth iteration, e.g. (from the docs),
The LOOP, WHILE and EACH tags allow you to define a BREAK attribute:
<tmpl_loop bingo break="3"> <tmpl_var _ ><if __break__>\n</if></tmpl_loop>
$htc->param(bingo => [qw(X 0 _ _ X 0 _ _ X)]);
outputs
X 0 _
_ X 0
_ _ X
(Code for above maple trees/rotten logs example:)
#!/usr/local/bin/perl
use strict;
use warnings;
use HTML::Template;
DEMO_A: {
my $text = <<_end_;
0:[<TMPL_LOOP forest>
[<TMPL_LOOP trees><TMPL_VAR type></TMPL_LOOP>] trees
[<TMPL_VAR logs>] logs
</TMPL_LOOP>]
_end_
my $tmpl = HTML::Template->new(
scalarref => \$text,
global_vars => 1,
die_on_bad_params => 0,
);
$tmpl->param( trees => [{ type => 'maple' }] );
$tmpl->param( forest => [{ logs => 'rotten' }] );
print $tmpl->output;
}
DEMO_B: {
my $text = <<_end_;
1:[<TMPL_LOOP forest>
[<TMPL_LOOP trees><TMPL_VAR type></TMPL_LOOP>] trees
[<TMPL_VAR logs>] logs
</TMPL_LOOP>]
2:[<TMPL_LOOP trees>[<TMPL_VAR type>]</TMPL_LOOP> trees]
_end_
my $tmpl = HTML::Template->new(
scalarref => \$text,
global_vars => 1,
die_on_bad_params => 0,
);
$tmpl->param( trees => [{ type => 'maple' }] );
$tmpl->param( forest => [{ logs => 'rotten' }] );
print $tmpl->output;
}
__END__
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
Html-template-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/html-template-users