简体   繁体   English

如何在Smarty中使用变量变量?

[英]How to use Variable variables in Smarty?

I want to use variable variables in smarty. 我想在smarty中使用变量变量。 I know that we can use it in PHP . 我知道我们可以在PHP中使用它。 But I am unable to find any way to achieve the same in Smarty template file. 但是我找不到在Smarty模板文件中实现相同效果的任何方法。 Consider the below scenario. 请考虑以下情形。

I have multiple variables which I pass from PHP file to Smarty tpl file. 我有多个变量,这些变量从PHP文件传递到Smarty tpl文件。 All these variables name have some similar pattern. 所有这些变量名称都有一些相似的模式。 eg $test_1 , $test_2 , $test_3 and so on. 例如$test_1$test_2$test_3等。

This is how, actually I am trying to achieve it. 这就是我实际上正在尝试实现的方式。 Here, $COUNTER represents 1, 2, 3.... 在这里, $COUNTER代表1、2、3 ...

 {$SELECTED_VALUE = "test_{$COUNTER}"}
 {$$SELECTED_VALUE|@print_r}

But when I try to print it out, it gives me error 但是当我尝试打印出来时,它给了我错误

Syntax Error in template "test.tpl" on line 127 "{$$SELECTED_VALUE|@print_r}" - Unexpected "$", expected one of: "{" , "identifier"

Now, in PHP, I can get the values of these variables using double $$ symbols. 现在,在PHP中,我可以使用双$$符号来获取这些变量的值。 But I am unable to find any way to achieve the same in a smarty tpl file. 但是我无法在智能的tpl文件中找到任何方法来实现相同的目的。

I have gone through these links, but couldn't understand anything out of it. 我已经通过这些链接,但无法理解其中的任何内容。

Variable Variable in Smarty Templates Smarty模板中的变量变量

Dynamics variables in smarty in loop Smarty in Loop中的动力学变量

Kindly guide me here, if it is possible. 如果可以的话,请在这里引导我。

you can write a loop in your tpl file below For example, lets say $count = 10; 您可以在下面的tpl文件中编写一个循环。例如,假设$ count = 10;

     {for $foo=1 to $count}
          <li>{$foo}</li>
     {/for}

Ok, it seems that I have found the solution. 好的,看来我已经找到了解决方案。 As I have mentioned above, I have these dynamically created and assigned $test_1 , $test_2 , $test_3 ,.... to smarty tpl file. 如上所述,我已经动态创建了这些$test_1 ,并将$test_1$test_2$test_3 ,...分配给了smarty tpl文件。 So to use these variables dynamically, I have taken followed approach. 因此,为了动态使用这些变量,我采用了以下方法。

{for $counter=1 to $total}
      {$test_{$counter}}
 {/for}

Thanks for help. 感谢帮助。

This should do what you are looking for: 这应该可以满足您的需求:

{$x = ['foo', 'bar']}
{${$x}|print_r}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM