Variable variables
| From: | Martijn van Zal | Date: | Wed, 05 Jun 2002 09:11:58 +0000 |
| Subject: | Variable variables | ||
| References: | 1 | Groups: | php.smarty.dev |
| Request: | Send a blank email to php-smarty-dev+get-109@lists.php.net to get a copy of this message | ||
Hello all...
I wonder if there is a way of implementing variable variabes into smart;
Sometimes I have an object, and I need a certain attribute from it. I need
this to create forms automatically. I have an array with form elements
(name, type) and I include the right template with the form for the right
variable type. ie: a textfield, wysiwyg html editor, image upload box,
selectbox
So what I do is I run through the form elements array like this
============================
{foreach from=$pageVars item=row}
<tr height=20>
{if (isset($row.editTemplate))}
{include file=$row.editTemplate row=$row}
{/if}
</tr>
{foreachelse}
============================
And in the editTemplate I do something like this:
============================
<td>{$row.name|translate}</td>
<td>
{if (isset($row.readOnly) && $row.readOnly)}
{$object|var:$row.name}
{else}
<input type="text" name="{$row.name}"
value="{$object|var:$row.name}">
{/if}
</td>
============================
The data to fill the form with comes from the '$object' object. What i want
to do is: $object->$row.name but unfortunally smarty currently doesn't have
this functionality. I solved this by using a modifier named 'var' which gets
the right attribute from the object
============================
function smarty_modifier_var($object, $var)
{
if (is_object($object))
return $object->$var;
return false;
}
============================
This works fine, but I don't think this is the correct method, it's more
like a hack to get what I want. I tried looking in the source code but it's
too complicated for me. I don't have any Idea if anyone else has the same
problems or if it is hard to change it in the compiler functions.
________________________________________
Martijn van Zal GFXartist Network
Technical Manager
mail: martijn@gfxartist.com
icq: 8213621