Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Nested sections and associative arrays can't be combined

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
edwin
Smarty n00b


Joined: 22 Jul 2003
Posts: 2

PostPosted: Tue Jul 22, 2003 12:28 pm    Post subject: Nested sections and associative arrays can't be combined Reply with quote

Sections can be nested and sections can use associative arrays, but it is not possible to nest a section in which the outer section uses an associative array.

Example nested section, which doesn't work:
(I want to use $clientArray[client].clientnr as the index for the inner section)

Code:

{section name=client loop=$clientArray}
    Clientname: {$clientArray[client].clientname}<br>
    Clientnumber: {$clientArray[client].clientnr}<br>
    Orders:<br>
    {section name=order loop=$orderArray[$clientArray[client].clientnr]}
        $orderArray[$clientArray[client].clientnr][order].ordernr<br>
    {/section}
{/section}


I tried all kinds of variations, to no avail.

The only solution is to make another array in PHP and use that as the outer section, but that:

1) is clumsy
2) breaks the coder-designer independancy

I suggest improving the loop attribute.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jul 22, 2003 1:11 pm    Post subject: Reply with quote

maybe sth. like this:
Code:

{section name=client loop=$clientArray}
     Clientname: {$clientArray[client].clientname}<br>
     Clientnumber: {$clientArray[client].clientnr}<br>
     Orders:<br>
     {assign var=clientnr value=$clientArray[client].clientnr}
     {section name=order loop=$orderArray.$clientnr}
         {$orderArray.$clientnr[order].ordernr}<br>
     {/section}
{/section}


works for you.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jul 22, 2003 6:41 pm    Post subject: Reply with quote

I find that Smarty's decoding of keys (especially via [] ) is not suitable for complicated expressions. Perhaps for a good reason; expressions like:
Code:
$orderArray[$clientArray[client].clientnr][order].ordernr

aren't as, let us say, transparent, as one would hope to see in a template.

You should probably consider using foreach as the outer loop since foreach can explicity handle associative arrays and furthermore, provides handy user named variables for loop iterations.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP