For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > May 2004 > Can I pass an array to sub templates via {include file}?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can I pass an array to sub templates via {include file}?
Tom

2004-05-11, 11:52 am

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?
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com