Hi, I have an array of 10 elements. I am using {section} to loop over this array. My requirement is to loop over this array and display only 2 elements out of 10. Next, i will have to loop over this array again and display remaining 8 elements. This is my sample code. {assign var='count' value=0} {section name=arr_loop loop=arr} {if meets the criteria and $count < 2} display {assign var='count' value=$count+1} {/if} {/section} {section name=arr_loop loop=arr} display only remaining 8 elements {/section} My questions are 1) In the first section, how do i break out of section as soon as i hit count 2 2) how can avoid duplications in the second section block? I don't want to display the 2 elements that are already covered in first section block. I am flexible to change the array definition to add flags to keep track of whats displayed in first section. However, all my attempts failed. I should also admit that i am just 2 days old with Smarty Template programming. Thanks
Be easier to code your own templating engine, it aint hard and smarty is not only buggy but overly complicated.
There is an easy way to do it with only one loop: {section name=arr_loop loop=$arr} {if $smarty.section.arr_loop.index < 2} display {else} display only remaining 8 elements {/if} {/section} Code (markup):
Greg Carnegie: Thanks for your response. It took me sometime to check back on your response. Again, the first 2 elements of the array may not meet my criteria for display. These 2 elements can be any 2 elements in that array. I would prefer just one loop too, but cannot with my lack of smarty knowledge. Hwever,i would like to know, if i can change the value of an array element from within smarty? As soon as i display an element, can i somehow set a flag? for example: {assign var='count' value=0} {section name=arr_loop loop=$arr} {if $arr[arr_loop].'textlength > 200 && $count < 2} display { $arr[arr_loop].displayed = 1} <=== Is this possible? It didn't work for me. {assign var='count' value=$count+1} {/if} {/section} {section name=arr_loop loop=$arr} {*Remaining 8 elements} {if $arr[arr_loop].displayed == 0} display {/if} {/section} itnashvilleCOM: Thats not an option anymore for me. Thanks