mmanso has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I'm using HTML::Template::Compiled on a project and I'd like to use the TMPL_EACH tag. Anyone did that? From it's name I guess it will allow me to itterate over an hash and have access to the key / value, correct? I just can't figure out how and the documentation doesn't help. This works:
<TMPL_EACH .res.YEARS> <TMPL_VAR .res.YEARS.2008><br> </TMPL_EACH>
But it assumes I know that there's a "2008" key. I'd like to do something like:
<TMPL_EACH .res.YEARS> Key: <TMPL_VAR .res.YEARS.key><br> Value: <TMPL_VAR .res.YEARS.value><p> </TMPL_EACH>
Thanks in advance.

Replies are listed 'Best First'.
Re: HTML::Template::Compiled - TMPL_EACH
by tinita (Parson) on Sep 05, 2008 at 08:13 UTC
    like the others said, it's <TMPL_VAR __key__ > <TMPL_VAR __value__>
    I just saw that for this tag I only have documentation in the reference, but not the main documentation. I'll note this for the next release.
Re: HTML::Template::Compiled - TMPL_EACH
by mmanso (Initiate) on Sep 05, 2008 at 08:27 UTC
    Thanks guys! I was missing:
    loop_context_vars => 1
    too... so, with the __key__, __value__ and that parameter, everything works like a charm. Again, thanks for the help.
Re: HTML::Template::Compiled - TMPL_EACH
by Anonymous Monk on Sep 05, 2008 at 07:05 UTC
      Here's the template with more traditional markup:
      <TMPL_EACH res> <TMPL_VAR __key__ > => <TMPL_VAR __value__ > </TMPL_EACH>
Re: HTML::Template::Compiled - TMPL_EACH
by mmanso (Initiate) on Sep 05, 2008 at 22:28 UTC
    After I've spend all day working with TMPL_EACH, I'm impressed...

    One feature missing is the ability to "sort by keys" when we use TMPL_EACH.

    Is this possible?

      ability to "sort by keys" when we use TMPL_EACH.
      I've thought about that. I was actually thinking about to sort by default, so I would change it internally to a loop over keys(). One wouldn't use the unsorted output anyway in production, I guess. What about an attribute CMP which can have the values num or alpha?

      edit: I just coded it, it's an easy change. <%each hash sort=num %>...
      Now one would probably like to have the possibility to reverse the sort order and to sort by values. I'm open to any suggestions on this.

        Hi there,

        You could have something like:

        <%each hash sort=num reverse=1 %>

        Meaning that by default it wouldn't reverse.

        The solution of having sort=num or alpha looks good to me.