Re: [SMARTY] Using (or not) associative arrays
| From: | Per Nystrom | Date: | Thu, 06 Mar 2003 03:50:46 +0000 |
| Subject: | Re: [SMARTY] Using (or not) associative arrays | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-8424@lists.php.net to get a copy of this message | ||
I don't think {foreach} will help me here. I still can't fetch the
associated keys in the array, which is the same shortcoming as {section}.
Given this array (the one in my first post is not quite what I meant to
use):
$form = array('Username' => array(
'type' => 'text',
'size' => '10',
'maxlength' => '80',
'name' => 'userid',
'value' => ''),
'Password' => array(
'type' => 'password',
'size' => '10',
'maxlength' => '80',
'name' => 'password',
'value' => ''));
I want to be able to loop through it and
generate output based on the keys and values, so the end result is
something like:
<table border="1" cellspacing="2" cellpadding="5">
<form action="action.php" method="POST">
<p><tr>
<td>Username<input type="text" size="10" maxlength="80"
name="userid" value=""></td>
<td>Password<input type="password" size="10" maxlength="80"
name="password"></td>
</tr> </form> </table>
The quoted values inside <input> come from the array, but the attributes
themselves and the form field names should ALSO come from the array, and that's the
tricky part. I want a template that looks like this:
{section} {* or foreach, I don't think it makes much diff *}
{* Outer loop, prints a tag for each cell *}
<td>{* code to reference the key value of $form[outer_loop_index] *}
<input
{section} {* index on $form[outer_loop_index][inner_loop_index] *}
{* Inner loop, prints input attribute assignments *}
{* code to reference the key value of
$form[outer_loop_index][inner_loop_index] *}
="{* code to reference the array value of
$form[outer_loop_index][inner_loop_index] *}"
{/section}
></td>
{/section}
On Wed, 05 Mar 2003 18:14:40 +0000, schultz wrote:
>
>
>
>
> Quoting Per Nystrom <centaur@netmagic.net>:
>
>> I've come to understand that Smarty templates can't retrieve the
>> key
>> values in associative arrays, so I have to flatten them. If my
>> understanding so far is correct, then what I'm trying to do is
>> probably a
>> nightmare in Smarty.
>>
>>
>>
>> I'm sure that others have faced this before, and there has to
>> be a better way. I'm wondering now if it's even worth it to try to
>> make a
>> generic form template. Has anybody found an elegant solution to
>> this?
>>
>> Thanks,
>> Per
>>
>>
> Wouldn't it just be easier to use a foreach loop which then you can
> acccess the associative array?
> http://smarty.php.net/manual/en/language.function.foreach.php