Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Get Smarty variable dynamically

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bene82
Smarty n00b


Joined: 09 Sep 2013
Posts: 1

PostPosted: Mon Sep 09, 2013 7:56 am    Post subject: Get Smarty variable dynamically Reply with quote

I have these variables:
Code:
$idben47    Smarty_Variable Object (3)
->value = "0.00"
->nocache = false
->scope = "Smarty root"
$idben48    Smarty_Variable Object (3)
->value = "120.00"
->nocache = false
->scope = "Smarty root"
$idben49    Smarty_Variable Object (3)
->value = "0.00"
->nocache = false
->scope = "Smarty root"


How I can dynamically get these in my tpl file?

In my tpl file I already have the $id_attribute variable (47,48,49 in this case). I'm trying to do something like this:
Code:

<span>Prezzo:{$idben.id_attribute}</span>


But the system don't get the variable...

Thank you in advance
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7293
Location: Lincoln Nebraska, USA

PostPosted: Mon Sep 09, 2013 1:33 pm    Post subject: Reply with quote

Do you have flexibility how things are assigned to the template? If so, you are better off structuring your data in a way that does not require dynamic variable naming. Such as:

Code:
$smarty->assign('idben' => array(
  '47' => '0.00',
  '48' => '120.00',
  '49' => '0.00'
));


Then in template:

Code:
{$idben.$id_attribute}


Otherwise you could assign everything under a variable so you can use:

Code:
{assign var="tmp" value="idben23"}
{$myarray.$tmp}


Otherwise you will have to use a plugin to do the variable variable thing, which there are some on the forum if you search.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4692
Location: Hamburg / Germany

PostPosted: Mon Sep 09, 2013 3:37 pm    Post subject: Reply with quote

Smarty 3 does support variable variable names. In your examole you could use
Code:
{$idben{$id_attribute}}
Back to top
View user's profile Send private message
anaas5
Smarty n00b


Joined: 04 Oct 2013
Posts: 1

PostPosted: Fri Oct 04, 2013 9:40 am    Post subject: Reply with quote

Knowledgeable post!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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