I've got some code where I create an HTML::Template object at the top, and then later on, may want to use the options loop_context_vars or global_vars depending on conditionals.

There's nothing in the POD about doing it that, way. But by dumping the HTML::Template object, I could see that these could be accessed like this:

$t->{options}->{loop_context_vars} = 1; $t->{options}->{global_vars} = 1;
interestingly, it works for loop_context_vars but not global_vars

Is there a reason for this? Can I do it a different way? Or alternatively, should I just turn them both on, even if they're not needed, and not try and do it conditionally? I was just being naturally conservative about turning on features which wouldn't be needed, that's all. Is there any kind of speed or performance implication to using these features?

Example code:

use HTML::Template; $t = HTML::Template->new( filehandle => *DATA, die_on_bad_params => 0 ); $t->{options}->{loop_context_vars} = 1; # turned on so I can use <tmpl_if name="__first__"> $t->{options}->{global_vars} = 1; # turned on so I can have a global var appear inside a loop $t->param( foo => [ { baz => 1 }, { baz => 2 }, { baz => 3 } ] ); # some loop data $t->param( bar => 'global var!' ); # a global variable print $t->output(); __DATA__ <tmpl_loop name="foo"> <tmpl_var name="bar"> <!-- this doesn't appear --> <tmpl_if name="__first__"> first! </tmpl_if> * <tmpl_var name="baz"> </tmpl_loop>

In reply to Setting HTML::Template options after new() by Cody Fendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.