【问题标题】:Smarty: string variable contains bracketsSmarty:字符串变量包含括号
【发布时间】:2012-03-24 04:44:20
【问题描述】:

我有一个关联数组分配给 Smarty 变量,如下所示:

$foo = array('my[content][hello]' => 'hello', 'my[content][goodbye]' => 'goodbye', ...

在模板文件中,如何访问hello?这失败了:

{$foo.my[content][hello]}

我猜这是失败的,因为括号意味着我正在尝试访问数组的一部分。在这种情况下,我不是。括号只是变量名的一部分。想法?

【问题讨论】:

    标签: smarty smarty3 smarty2


    【解决方案1】:

    试试

    {$foo.{'my[content][hello]'}}
    

    {$foo['my[content][hello]']}
    

    或(如果所有其他方法都失败了[意味着您仍在 Smarty2 上]):

    {assign var=key value='my[content][hello]'}
    {$foo.$key}
    

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2019-11-25
      • 2017-08-04
      • 1970-01-01
      相关资源
      最近更新 更多