Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Keep It Simple, Stupid
 
PerlMonks  

Template Toolkit foreach loop

by Dr3am3rz (Initiate)
on Mar 07, 2013 at 03:25 UTC ( #1022138=perlquestion: print w/ replies, xml ) Need Help??
Dr3am3rz has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I am currently using Template Toolkit and have never learn or use before TT. And now I'm really really stuck and have no clues at all.

For example, I have 10 files, 5 files dated year dd/mm/2011 and 5 files dated dd/mm/2012. I have tried using foreach loop to get the year of each files, after that I'm stuck.

What I need to achieve is to get the year of the files and using that to create a link to display those documents on that year.

Example:
Group by year: 2012 | 2011
When the user click either one of them, it will show the respective files.

Hope you guys understand. Please some kind souls out there help me out. =x Thanks in advance.

Comment on Template Toolkit foreach loop
Re: Template Toolkit foreach loop
by rnewsham (Friar) on Mar 07, 2013 at 10:34 UTC

    Without a dump of the data which is being sent to template toolkit or a clearer description of how you want them grouped it is difficult to say.

    Assuming you have an array of hashes named files, with date as a key in each hash, containing the date as formatted in your question. You could do something like this.

    [% FOREACH file=files %] [% IF ( matches = file.date.match('dd/mm/(\d+)') ) %] <a href='#'>[% matches.0 %]</a> [% END %] [% END %]

      I tend to try to put logic concerns in the application, and presentation concerns in the template.   Usually, one subroutine is called to build the page.   It assembles information in its local variables, sometimes also defines a “closure” (a code-ref) that the template will use to withdraw the information that it needs, then invokes the template.   The information it needs is prepared in advance.   The template drives how it looks, and if Marketing wants to completely re-design that, the program won’t break.

Re: Template Toolkit foreach loop
by Anonymous Monk on Mar 07, 2013 at 10:39 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-04-18 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls