Re: [SMARTY] Dynamic Variables
| From: | Monte Ohrt | Date: | Wed, 01 Jun 2005 13:20:33 +0000 |
| Subject: | Re: [SMARTY] Dynamic Variables | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-12650@lists.php.net to get a copy of this message | ||
You can get around this with a temporary assign:
{assign var="pagekey" value="page_key_
$DIGIT_value"}
{$page_obj->$pagekey}
But why not make easier... instead of:
$type_varname = 'page_key_' . $digit. '_type';
$this->{$type_varname} = $rs->fields['page_key_' . $digit . '_type'];
Do something like:
$this->page_key_type[$digit] = $rs->fields['page_key_' . $digit . '_type'];
Then in the template instead of creating dynamic var names, just use the key name:
<input name="page_key_{$digit}_value" value="{$page_obj->page_key_type.$digit}" size="10" />
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" 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