Can I pass an array to sub templates via {include file}?
| From: | Tom | Date: | Tue, 11 May 2004 14:07:16 +0000 |
| Subject: | Can I pass an array to sub templates via {include file}? | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-11377@lists.php.net to get a copy of this message | ||
Hello again :)
I have a structure of templates where some templates are shared by other templates [and scripts] (eg. an 'address' template) and so I end up with templates that may look a little like this:
person.tpl
<form action="update.php">
<p>Home address:
{include file="address.tpl" prefix="home" source="
$person.address"}
<p>Work address:
{include file="address.tpl" prefix="work" source="$person.address"}
<input type="submit">
</form>
__END__
address.tpl
<input name="{$prefix}number" value="{|$source.prefix.number|}">
<input name="{$prefix}street" value="{|$source.prefix.street|}">
<input name="{$prefix}postcode" value="{|$source.prefix.postcode|}">
__END__
display.php
$person['address']['home'] = array('number'=>'12', 'street'=>'London Road', 'postcode'=>'W3');
$person['address']['work'] = array(number'=>'13', 'street'=>'London Road', 'postcode'=>'W3');'
$smarty = new Smarty();
$smarty->assign('person', $person);
$smarty->display('person.tpl');
__END__
Now, much as I expected this bit:
source="$person.address"
does not work as it is a string argument (not an array) according to the online documentation.
I also tried
{assign val="source" value="$person.address"}
But this is the same (string) problem, and even if I could somehow, that seems a bit kludgy and not quite what I want. If I add further similar templates on the same page (eg personal details) then I would be re-assigning the source again later in the same template:
{assign val="source" value="$person.detail"}
As the depth of the data and usage of templates within templates, I need to be able to control the layout from the 'master' templates whilst just assigning variables in the display scripts that have a structure that associates with the data relationships...
I really didn't want such a complicated structure, but trying to simplify it has meant that it doesn't have the necessary scope to deal with the project design :(
However, Smarty does something similar somewhere, as it happily has this type of array depth within the {foreach} and {section} functionality...
Any suggestions?
Or have I just gibbered myself into your "He's crazy!" mail filter?