Re: Section and Associative Arrays
| From: | Thomas Schulz | Date: | Fri, 06 Dec 2002 20:44:02 +0000 |
| Subject: | Re: Section and Associative Arrays | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-6679@lists.php.net to get a copy of this message | ||
Malcolm Hutty schrieb in php.smarty.general:
> Is it possible to get a section to iterate over an associative array?
>
> I've got one deeply multi-dimensional associative array which
> describes all the data I want Smarty to render into the template. As a
> new Smarty user, I assumed this would be a common/key use for Smarty.
My answer is a sample.
I use Smarty to render the multi-dimensional associative array which is
delivered by PEAR's HTML_Quickform->toArray():
Array
(
[validationScript] =>
[attributes] => action="/index.php" method="POST"
target="_self"
[requiredNote] => wird benötigt!
[sections] => Array
(
[0] => Array
(
[header] => Registrierung (1.Schritt)
[elements] => Array
(
[username] => Array
(
[required] => 1
[html] => <input size="12"
name="username" type="text" />
[value] =>
[type] => text
[frozen] =>
[label] => Ihre Nutzerkennung
)
[fullname] => Array
(
[required] => 1
[html] => <input size="24"
name="fullname" type="text" />
[value] =>
[type] => text
[frozen] =>
[label] => Ihr vollständiger Name:
)
[email] => Array
(
[required] => 1
[html] => <input size="40"
name="email" type="text" />
[value] =>
[type] => text
[frozen] =>
[label] => Ihre E-mail Adresse:
)
[re_email] => Array
(
[required] =>
[html] => <input size="40"
name="re_email" type="text" />
[value] =>
[type] => text
[frozen] =>
[label] => Wiederholung:
)
[save] => Array
(
[required] =>
[html] => <input name="save"
value="Weiter" type="submit" />
[value] => Weiter
[type] => submit
[frozen] =>
[label] =>
)
)
)
)
)
The Smarty code to format this array:
-------------------------------------
{if $frm.validationScript}
<script language="javascript">
<!--
{$frm.validationScript}
//-->
</script>
{/if}
<table>
<form{$frm.attributes}>
{foreach item=sec from=$frm.sections}
<tr>
<td class="titel">{$sec.header}</td>
</tr>
<tr>
<td class="content">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" >
{if $frm.errors}
<b><font color="{#err#}">
Bitte korrigieren... Angaben!
</font></b>
{/if}
</td>
</tr>
{foreach key=name item=element from=$sec.elements}
<tr>
<td>
{$element.label}
</td>
<td>
{if $frm.errors[$name]}
<font color="{#err#}">{$frm.errors[$name]}</font>
<br />
{/if}
{$element.html}
</td>
</tr>
{/foreach}
</table>
</td>
</tr>
{/foreach}
</form>
</table>
Hope this is not too complex ;-)
ThS.
--
http://4bconsult.de