View previous topic :: View next topic |
Author |
Message |
gizzymo Smarty n00b
Joined: 09 Jun 2015 Posts: 2
|
Posted: Tue Jun 09, 2015 11:22 am Post subject: Get variable from top level using another variable |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jun 09, 2015 2:14 pm Post subject: |
|
|
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 |
|
gizzymo Smarty n00b
Joined: 09 Jun 2015 Posts: 2
|
Posted: Tue Jun 09, 2015 3:00 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jun 09, 2015 3:03 pm Post subject: |
|
|
gizzymo wrote: | you dont need to understand |
Thanks, I laughed. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jun 09, 2015 5:01 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Jun 10, 2015 2:28 pm Post subject: |
|
|
That assuming the other variable was also assigned, right? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jun 11, 2015 7:21 pm Post subject: |
|
|
AnrDaemon wrote: | That assuming the other variable was also assigned, right? |
This is what he did |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Jun 12, 2015 4:43 pm Post subject: |
|
|
I don't see, where he did it. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Jun 13, 2015 5:48 pm Post subject: |
|
|
He did assign the field_names array and the variables field1 and fiield2 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jun 14, 2015 9:55 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Jun 14, 2015 6:35 pm Post subject: |
|
|
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 |
|
|