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

Get variable from top level using another 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 -> General
View previous topic :: View next topic  
Author Message
gizzymo
Smarty n00b


Joined: 09 Jun 2015
Posts: 2

PostPosted: Tue Jun 09, 2015 11:22 am    Post subject: Get variable from top level using another variable Reply with quote

I am getting this error:L
Quote:

Syntax error in template "\webapp\tpl\index.tpl" on line 3 "{assign var="filter" value=$[$field_name]}" - Unexpected "[", expected one of: "{" , "identifier"


In smarty I have the following data available:

Code:
array(
    'field_names' => array('field1', 'field2')
    'field1' => 'test1',
    'field2' => 'test2'
)


I need to loop through the field_names and assign the field1 to $filter == test1

Code:
{foreach $field_names as $field_name}
    {assign var="filter" value=`$field_name`}
    {break}
{/foreach}


I know this works: {assign var="filter" value=$foo[$field_name]}

So I tried this but getting the error above: {assign var="filter" value=$[$field_name]}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jun 09, 2015 2:14 pm    Post subject: Reply with quote

I don't understand, why you have this issue at all. Not ot mention, why you are doing it this way, to begin with.
It seems to me that you are going through rather silly loops with your application.
If you want to have variable "$filter" to have specific content within the template, why not assign it directly?
Your example template code gives little insight into your issue.

P.S.
The right syntax would be {assign ... value=$field_name}, but only syntax. The use case you present is just plain wrong.
Back to top
View user's profile Send private message
gizzymo
Smarty n00b


Joined: 09 Jun 2015
Posts: 2

PostPosted: Tue Jun 09, 2015 3:00 pm    Post subject: Reply with quote

I've simplified it down because you dont need to understand the applicaiton only what I am trying to do in smarty.

In php I would do this:
Code:
foreach($field_names as $field_name){
    $flter = ${$field_name};
}


Is this possible in smarty?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jun 09, 2015 3:03 pm    Post subject: Reply with quote

gizzymo wrote:
you dont need to understand

Thanks, I laughed.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Jun 09, 2015 5:01 pm    Post subject: Reply with quote

In Smarty the variable part of a variable name must be enclosed in { }.
Like $foo{$bar}buh or in you case ${$field_name}

[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jun 10, 2015 2:28 pm    Post subject: Reply with quote

That assuming the other variable was also assigned, right?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Jun 11, 2015 7:21 pm    Post subject: Reply with quote

AnrDaemon wrote:
That assuming the other variable was also assigned, right?


This is what he did
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jun 12, 2015 4:43 pm    Post subject: Reply with quote

I don't see, where he did it.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Jun 13, 2015 5:48 pm    Post subject: Reply with quote

He did assign the field_names array and the variables field1 and fiield2
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 14, 2015 9:55 am    Post subject: Reply with quote

Damn. I missed that part from the start.
But my question still stands - why he's doing it that way?
Why not just use foreach()/{$item@key} directly?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Jun 14, 2015 6:35 pm    Post subject: Reply with quote

AnrDaemon wrote:
Damn. I missed that part from the start.
But my question still stands - why he's doing it that way?
Why not just use foreach()/{$item@key} directly?


Looks like he used $smarty->assign(array(....));

So he has 3 template variables : field_names, field1 and field2.

Of cause he could assign the variables in another way, but I think this is just an example.
Back to top
View user's profile Send private message
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 -> 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