Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Issue/Problem using dynamic assigned variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Frameworks
View previous topic :: View next topic  
Author Message
Demonknight
Smarty n00b


Joined: 29 Oct 2009
Posts: 1

PostPosted: Thu Oct 29, 2009 1:18 pm    Post subject: Issue/Problem using dynamic assigned variable Reply with quote

Hi!
My first posting for years:
New at using the smarty framework and have following problem:
I wanna use assigned template variables whose names are dynamically created.
By trying to use them I fail:
snippet:
---within loop---
{assign var="value_id" value="value_"|cat:$item->id}
{$value_id} // ->just output to check name -> name is exactly what I want
${${$value_id}} // -> hoped to get value of the variable which has the name {$value_id}
---loop end----

unfortunatly it ends with the ....syntax error: unrecognized tag: ${$value_id (Smarty_Compiler.class.php, line 446).... exception

A hint would be very appreciated!
greetings,
Demonknight Question
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Fri Nov 06, 2009 10:18 pm    Post subject: It is possible Reply with quote

I am not exactly sure, why you would need to have this kind of requirement. But it is possible in this way:
Code:
Case #1
{assign var='price' value='90.00'}
{assign var='value_id' value='price'}
{steal tag=$value_id}

Case #2
{assign var='size' value='XL'}
{assign var='dimension' value='size'}
{steal tag=$dimension}

Output:
Code:
Case #1
90.00

Case #2
XL

I needed to write a function, that seeks for some other template variables, based on its name. Here is the code:

function.steal.php
Code:
<?php
/**
* Steals a value of another template variable
* @author Bimal Poudel
*/
function smarty_function_steal($params=array(), &$smarty)
{
   $stolen_value = null;
   if(!empty($params['tag']))
   {
      if(isset($smarty->_tpl_vars[$params['tag']]))
      {
         $stolen_value = $smarty->_tpl_vars[$params['tag']];
      }
   }
   return($stolen_value);
}
?>


You can assign the returnable value into another template variable and print, if you need more flexibilities. But things would go more complex.

Does this help you any?
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Frameworks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP