Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Template Toolkit - problem getting data out array - AoHoA

by GertMT (Friar)
on Mar 07, 2014 at 17:38 UTC ( #1077427=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I created an Array of Hashes whereby two hashes again have an array. Now I'm having trouble getting this working in Template Toolkit!
I found out how to get 1 photo out.

How to get all three (or as many that are there) in my HTML?

Thanks, Gert
$VAR1 = [ { 'filenameLarge' => [ 'fIyvYwJWmstR.jpg', 'koeSHYEaekfv.jpg', 'CtDKXyiRXhWz.jpg' ], 'pos' => 1, 'title' => 'February event (Feb 2013)', 'link' => 'february-event-feb-2013', 'bookid' => 8, 'filenameSmall' => [ 'lAwKmMFJVQDI.jpg', 'mGNLDhwWEBHN.jpg', 'oREpowZBMYne.jpg' ] },
HTML - TT code
[% FOREACH e IN fotos %] [% e.title %] <img src="/photo/[% e.bookid %]/tn/[% e.filenameSmall.1 %]" /> [% END %]

Replies are listed 'Best First'.
Re: Template Toolkit - problem getting data out array - AoHoA
by Your Mother (Archbishop) on Mar 07, 2014 at 19:51 UTC

    I think your structure is fine. You just have to think more “perlishly” for your iteration. This should get you on the path to what you want–

    use strictures; use Template; my $stuff = [ { 'filenameLarge' => [ 'fIyvYwJWmstR.jpg', 'koeSHYEaekfv.jpg', 'CtDKXyiRXhWz.jpg' ], 'title' => 'February event (Feb 2013)', 'bookid' => 8 } ]; my $tt = Template->new; $tt->process(\*DATA, { fotos => $stuff }) || die $tt->error(), "\n"; __DATA__ [%-FOR e IN fotos %] [%-e.title %] [%-FOR i IN e.filenameLarge %] <img src="/photo/[% e.bookid %]/tn/[% i %]" /> [%-END %] [%-END %]

    Yields–

    February event (Feb 2013) <img src="/photo/8/tn/fIyvYwJWmstR.jpg" /> <img src="/photo/8/tn/koeSHYEaekfv.jpg" /> <img src="/photo/8/tn/CtDKXyiRXhWz.jpg" />
      pfff... That is great!!

      After so much time getting this structure in place (that is maybe a post for some other time) I thought to have tried every possible trick to get this working. You found me the solution and that makes my day (weekend even!).

      Thank you,
      Gert
Re: Template Toolkit - problem getting data out array - AoHoA
by MidLifeXis (Monsignor) on Mar 07, 2014 at 19:10 UTC

    It seems to me that your data structure is a little clumsy for what you are trying to do. You are maintaining two separate arrays of file names. If I were to do this, I would probably use a data structure something like:

    $VAR1 = [ { 'filenames' => [ { large => 'fIyvYwJWmstR.jpg', small => 'lAwKmMFJVQDI. +jpg', } ... ], 'pos' => 1, 'title' => 'February event (Feb 2013)', 'link' => 'february-event-feb-2013', 'bookid' => 8, },

    HTH.

    --MidLifeXis

Re: Template Toolkit - problem getting data out array - AoHoA
by crusty_collins (Friar) on Mar 07, 2014 at 18:28 UTC
    i dont know about Template Toolkit but this is the way i do it.
    foreach (@{$fotos->{filenameLarge}}){ print $_; }
      Thanks, I need to use the TT so I'll change the data structure to get it to work. I definitely like to be able to use different paths and probably create a separate data-structure.
Re: Template Toolkit - problem getting data out array - AoHoA
by sundialsvc4 (Abbot) on Mar 08, 2014 at 03:01 UTC

    One more small thing to consider, also:   “sometimes the underlying data-structures are closely matched to the needs of the template, and sometimes they aren’t.”   When the structure is complicated, or there are a number of alternatives for displaying it to the user, it might not be desirable to have code within the template be highly dependent on that structure.   (It becomes something that now must be changed in many scattered places.)

    So, in cases like that, you can define functions, or “closures,” which can be referenced from within the template, as variables that are provided to it by the caller of the template.   Each time the template refers to it, the underlying function (in the Perl module, and/or in a common package referred-to by the same) does the work.   Now, if the data structure changes or evolves, only the Perl module has to be modified or expanded.

      for now I'm overwhelmed by all possibilities of TT The structure will without doubt change so this feature could help. Thanks
Re: Template Toolkit - problem getting data out array - AoHoA
by GertMT (Friar) on Mar 07, 2014 at 18:30 UTC
    ah well,

    Responding myself... guess I painted myself in a corner here. Having an extra variable in the path where the array should be used is a problem. Without this flexible path it worked like...

    [% FOREACH e IN artikelen.photo %] </photo/[% e %]" alt="" /> [% END %]
    I'll have to change a few earlier steps to make it work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1077427]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-10-20 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (81 votes). Check out past polls.

    Notices?