Re: [SMARTY] Deep access of associative array value by dynamic index.
| From: | Jochem Maas | Date: | Thu, 29 Apr 2004 18:19:18 +0000 |
| Subject: | Re: [SMARTY] Deep access of associative array value by dynamic index. | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11324@lists.php.net to get a copy of this message | ||
regardless of the fact that you solved the problem I thought it might be interesting to point out that using an object might make things a little
easier on the eyes/brain.
class TomObj
{
var $selected;
var $allowedit;
var $selection = array (42=>'small',10=>'medium',24=>'large');
function TomObj($selected, $readonly = false)
{
$this->selected = $selected;
$this->allowedit = !(boolean)$readonly;
}
}
$TObj = new TomObj( 10 );
// assign $TObj
// then in the template...
{$Tobj->selected}
// and ...
{if $Tobj->allowedit}
{/if}
// and ...
{foreach from=$Tobj->selection key=size item=name}
{if $size == $TObj->selected}
{/if}
{/foreach}
Tom wrote:
Hello I need some help with making good use of associative arrays in Smarty. I have an array created that might partly look something like this: $foo['building']['selected'] = 10; $foo['building']['selection'] = array ( 42=>'small',10=>'medium', 24=>'large');$foo['allowEdit'] = 'no'; The script then simply assigns: $smarty->assign('foo', $foo); And and my template contents is a little like this: <html> <body> Size of building is : {if $foo.allowEdit == 'yes'} <select name="buildingSize">{html_options options=</select> {else} {$foo.building.selection[$foo.building.selected]} {/if} </body> </html> Which (obviously) doesn't work. Can anyone help? :) MTIA Tom$foo.building.selectionselected=$foo.building.selected}