Re: [SMARTY] Dynamic Variables

From: Date: Tue, 31 May 2005 22:34:35 +0000
Subject: Re: [SMARTY] Dynamic Variables
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12648@lists.php.net to get a copy of this message
Stephen J. Lawrence Jr. wrote:
Well, I have 12 different sets of variables on this page that I display in a form. The html is all identical except for the variable names. I am trying to keep from having to hardcode 12 different HTML TR's for each of the vars. On the PHP side, I have done this:
       $this->digits = array('1','2','3','4','5','6','7','8','9','0','aa','bb','cc');
       foreach($this->digits as $digit)
       {
           $type_varname = 'page_key_' . $digit. '_type';
           $this->{$type_varname} = $rs->fields['page_key_' . $digit . '_type'];
      }
Then I want a foreach in the template to spin through each of the variables. {foreach from=digits item=$digit} <input name="page_key_{$digit}_value"
why not {$page_obj->getPageKeyValue($DIGIT)} where the following method is defined in your object: function getPageKeyValue($d) { if (!in_array($d, $this->digits) return null; return $page_obj->{"page_key_$d_value"}; } obviously untested, hth.
value="{$page_obj->page_key_$DIGIT_value}" size="10" /> {/foreach} the "name=page_key_{$digit}_value" part works fine, but the "value=" part does not. The first loopthrough, the value needs to be $page_obj->page_key_1_value, the second through needs to be $page_obj->page_key_2_value, etc.. steve Bill Cunningham wrote:
Stephen, That was a much clearer explaination of what you're looking for. It appears that perhaps you want an array there. Something like: $my_obj->var[$cnt] ?? What about: $var = "variable_1_value"; $my_obj->$$var ?? <<- not sure if a variable indirection works in a template though. Maybe it would help if you told us exactly why you are trying to achieve this. Are you trying to auto create a bunch of templates? - Bill


« previous php.smarty.general (#12648) next »