如何调用由 2 个字符串组合而成的变量
我有这个:
string {$one} = "$hello_"
string {$two} = "world"
如何从上述两个字符串变量中调用变量 $hello_world
? capture
对我不起作用。
使用Smarty v2.5
I have this:
string {$one} = "$hello_"
string {$two} = "world"
How can I call the variable $hello_world
from the above two string variables?capture
did not work for me.
Uses Smarty v2.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 文档 中:
所以,您想要:
因为此功能不是不允许,我建议使用 smarty 插件 来模仿该行为你想要的。模板插件只是简单的 php 函数,通过
$smarty-> 调用。 loadPlugin()
方法。From the documentation:
So, you want:
Since this functionality isn't allowed, I would recommend using a smarty plugin to mimic the behavior you want. Template plugins are just simple php functions, called via the
$smarty->loadPlugin()
method.Smarty 2.x 不支持可变变量。
smarty 中的变量实际上存储在 smarty 对象内,因此您需要 Smarty 中的显式支持才能使用方便的标准变量-变量语法。
以下是我在 Smarty 2.x 中能想到的最好的。它使用 PHP 块来为您存储组合结果的值。
但是,如前所述,您必须删除
$one
值开头的$
。否则,您需要将其修改为以下行:Smarty 2.x doesn't support variable variables.
Variables in smarty are actually stored inside the smarty object so you'd need explicit support in Smarty to use the convenient standard variable-variable syntax.
The following is the best I could come up with in Smarty 2.x. It uses a PHP block to store the value of the combined result for you.
As mentioned, however, you have to get rid of the
$
at the beginning of the value of$one
. Otherwise you will need to modify it to the following line:不过,{${$foo}{$bar}} 只能在 Smarty3 中使用。在 Smarty2 中,您必须为此编写一个插件(或者只需搜索 smarty 论坛,因为那里有很多解决方案......)
{${$foo}{$bar}} will only work in Smarty3, though. In Smarty2 you'd have to write a plugin for that (or simply search the smarty forum, as there are plenty of solutions there…)