in reply to HTML::Template Loop Question

I often find it helpful to think about the template first and then see what the data structure that fills it would have to look like. This is how I'd write a template for the output you want:
<ul> <TMPL_LOOP states> <li> <TMPL_VAR state> <ul> <TMPL_LOOP people> <li> <TMPL_VAR person> </TMPL_LOOP> </ul> </TMPL_LOOP> </ul>
Now that we know where the loops and vars go, we can figure out what kind of data structure we need. TMPL_LOOPs need something like loop_name => [ { .. }, { .. }, .. ]. (A reference to an array of hash references). TMPL_VARs just need var_name => "scalar". So in order to fill out this template, you'll need data that is structured like this:
my @states = ( { state => "Alaska", people => [ { person => "Joe Blow" }, { person => "Peter TorkM" } ] }, { state => "California", people => [ { person => "Lily White }, { person => "Erik Svendater" } ] }, ... ); $tmpl->param( states => \@states );
If you have your data already in some other form, and have questions about how to get it into a data structure like this, let us know and we can probably help with that too..

blokhead

Replies are listed 'Best First'.
2Re: HTML::Template Loop Question
by jeffa (Bishop) on Sep 23, 2003 at 22:44 UTC
    And just in case the data was originally stored in a hash like so:
    my %state = ( Alaska => ["Joe Blow", "Peter Tork"], California => ["Lily White","Erik Svendater"], Florida => ["Fred Flinstone", "Barney Rubble", "Judy Jetson"], );
    Here is some code to munge that data into what HTML::Template needs:
    my @state = [ map { state => $_, people => [ map {person => $_}, @{$state{$_}} ], }, sort keys %state ];

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Re: HTML::Template Loop Question
by theAcolyte (Pilgrim) on Sep 23, 2003 at 22:53 UTC

    Thank you *SO* much. I made a test file with just your datastruct and the HTML you had above ... ran exactly as it should of.

    So its ... an array of hashes and arrays of hashes (I think). To be completely honest, I still don't understand precisely why it is working, but it is working ... I'll spend some time pondering it out later so that I can repeat it in different contexts.

    Again, Thanks!

    For sure ++

    - Erik

Re: HTML::Template Loop Question
by jonadab (Parson) on Sep 23, 2003 at 23:21 UTC
    <ul> <TMPL_LOOP people> <li> <TMPL_VAR person> </TMPL_LOOP> </ul>
    <nit>

    Not having used these templates myself yet, I may be spouting off without cause here, but shouldn't you close off the li?

    <ul> <TMPL_LOOP people> <li> <TMPL_VAR person> </li> </TMPL_LOOP> </ul>
    </nit>

    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
      www.w3.org says it's optional:
      The end tag for LI elements can always be omitted.

      blokhead

        That depends which spec you're working with if your working with any spec at all.

        XHTML requires closed tag pairs for <li></li>, <p></p>, <option></option> etc.

        --
        Clayton

        Ack! That was in what year? 1997? That document is at least four versions out of date! Backslashing literal @ characters in Perl strings was optional at one time too! If you go to w3.org and click on "HTML" then on "Recommendations", you will find that such ancient versions are no longer even listed. That's way past deprecated. (HTML 4.01 is deprecated "legacy" HTML; XHTML1.0 is obsolete, but not quite yet (or only very mildly) deprecated; XML1.1 is the current cutting edge.)


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

        But if you're using XHTML, www.w3.org also says:

        Documents must be well-formed

        Some/many/most (please choose your appropriate level of dogmatism) developers use XHTML.

        --
        bowling trophy thieves, die!

Re: Re: HTML::Template Loop Question
by theAcolyte (Pilgrim) on Sep 24, 2003 at 18:21 UTC
    I wanted to write another reply and say thanks again. Once I *really* studied the concrete example it started to make sense. Well, eventually.

    I was able to build a much more complicated HTML::Template datastructure this morning without too much difficulty (okay, a little, but hey, this has dogged me for 3 months now).

    I just love perlmonks.

    - Erik