Re: FOREACH question

Richard Thomas <[email protected]> Thu, 23 Aug 2012 22:56:08 +1000
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
For an array variable, the vmethod .size tells you the number of elements, eg 
[% light.size %]

For a hash, try [% light.keys.size %]

Hope that helps,
RET
________________________
Sent from my iPhone

On 21/08/2012, at 4:21 AM, Mark Haney <markh-rOuQeLG7NUhWk0Htik3J/[email protected]> wrote:

> I have a question regarding the FOREACH loops.  I have a FOREACH being used to loop through an array to insert various images into a table. Like this:
> 
>>    
>>        [% FOREACH light IN lights %]
>>            <td>
>>        <img src="[% light.light_red %]">
>>        <img src="[% light.light_yellow %]">
>>        <img src="[% light.light_green %]">
>>        <img src="[% light.light_blue %]">
>>            </td>
>>            
>>        [% END %]
> 
> What I need to do now is to loop through each element in the array (like a counter) and insert the images into it's own table.  Like this:
> 
> FOR a <= 3
>    <table>
>    <tr>
>        <td>
>    <img src="[% light.[$a] %]">
>        </td>
>    </tr>    
>    </table>
> a= a++
> NEXT
> 
> The problem is I don't know how to tell it the number of elements in the array (in this instance it will always be 4).
> 
> Keep in mind that this code is inside an HTML template, so I don't really want any perl in it if I can help it.
> 
> Any ideas?
> 
> 
> -- 
> 
> Mark Haney
> Software Developer/Consultant
> AB Emblem
> markh-rOuQeLG7NUhWk0Htik3J/[email protected]
> Linux marius.homelinux.org 3.5.1-1.fc17.x86_64 GNU/Linux
> 
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates