Re: [SMARTY] section loop keys / nested loops

From: Date: Wed, 09 Apr 2003 21:06:22 +0000
Subject: Re: [SMARTY] section loop keys / nested loops
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-9200@lists.php.net to get a copy of this message
I think the manual does mention it, but it could probably be clearer. I never use foreach, but I think you can't nest loops with foreach. Also, I believe section is implemented internally with PHP for(), and foreach is implemented with PHP foreach(). foreach() makes a copy of each element of the array, and is thus less efficient. cheers, Travis olinux wrote:
seems like a good idea. I guess the problem is that section works for indexed arrays only. Maybe the manual should mention this. What benefits does using section have over foreach? (seems foreach would be more flexible since it can take associative arrays) I like the sectionelse syntax rather than combining if/else i.e. {if $contacts != ""} {foreach from=$contacts item=contact}
    {$contact.name}
{/foreach} {else} No contacts {/if} Thanks to all who replied! --- Travis Low <travis@dawnstar.com> wrote:
My current preferred solution is to just stuff everything into the one array. In your case, just stuff the contacts into the offices array. Something like: # for each office, build contacts: $contacts['contact_id']['contact_name'] $contacts['contact_id']['contact_phone'] $offices['id'] $offices['city'] $offices['state'] $offices['id']['contacts'] = $contacts Then, in your template: {section name=office loop=$offices} {$offices[office].city}, {$offices[office].state}<br>
    {assign var="contacts"
value=$offices[office].contacts}
    {section name=contact loop=$contacts}
    -- {$contacts[contact].name} <br>
    {/section}
{/section} cheers, Travis
__________________________________________________ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://tax.yahoo.com
-- Travis Low <mailto:travis@dawnstar.com> <http://www.dawnstar.com>

« previous php.smarty.general (#9200) next »