RE: Nested Sections + Associative Array [SOLUTION]
| From: | Joris Trooster / Interstroom | Date: | Mon, 05 Mar 2001 08:36:34 +0000 |
| Subject: | RE: Nested Sections + Associative Array [SOLUTION] | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-298@lists.php.net to get a copy of this message | ||
Hi Jim,
Yes, you need a separate array for this. A lot of people allready tried to
use nested array's with no luck. Due to the design of Smarty it is not
possible. However with assigning a separate array to Smarty you can do the
trick (like the example in the quickstart).
The solution for your example (one of many):
test.php
<?
require_once('Smarty.class.php');
$branches[] = array(
'name' => 'My Name',
'address' => 'PO BOX 1234',
'location' => '1 Smarty Way',
'contacts' => array(
array('type' => 'Phone',
'number' => '123123' ),
array('type' => 'Phone',
'number' => '342232')
)
);
while( list($k,$v) = each($branches) ) {
$branche_array[$k]['name'] = $v['name'];
$branche_array[$k]['address'] = $v['address'];
$branche_array[$k]['location'] = $v['location'];
$branche_array[$k] = $v;
$contact_array[$k] = $v['contacts'];
}
$smarty = new Smarty;
$smarty->assign('details', $branche_array);
$smarty->assign('contacts', $contact_array);
$smarty->display('test.tpl');
?>
Note: You can leave the $branche_array[$k] out of the while loop, and assign
branches to details, but obvious you cannot use the nested contacts array
from the 'details' array.
test.tpl
{section name=outer loop=$details}
Name: {$outer/details.name}<br>
Postal Address: {$outer/details.postal}<br>
Location: {$outer/details.location}<br><br>
Contacts:<br>
{section name=inner loop=$outer/contacts}
{$outer/inner/contacts.type}: {$outer/inner/contacts.number}<br>
{/section}
{/section}
Output:
Name: My Name
Postal Address:
Location: 1 Smarty Way
Contacts:
Phone: 123123
Phone: 342232
Regards,
Joris Trooster