【问题标题】:How to use an smarty var inside another var?如何在另一个 var 中使用 smarty var?
【发布时间】:2012-06-19 06:57:08
【问题描述】:

我想在我的 foreach 循环中获得一个带有项目 id 的 var。 我想尝试的(只是演示代码)如下,但它只是给我一个错误。

php

$this->smarty->assign(array(
  'demo' => array(
    'foo_bar' => 'succeed!',
    'foo_seat' => 'succeed also!'
  ),
  'bar' => 'bar',
  'seat' => 'seat'
));

tpl

value="{$demo.foo_{$bar}}"

错误

Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "application/views/overzicht/selectie.tpl" on line 55 "{$demo.foo{$bar}}" - Unexpected "{", expected one of: "}"

谁有想法?

【问题讨论】:

    标签: php smarty var


    【解决方案1】:

    试试这个

    {assign var='property' value='foo_'|cat:$bar}
    
    {$demo.$property}
    

    【讨论】:

    • 遗憾的是,这为循环中的每个值占用了更多资源。但我想这是唯一的选择。让它这样工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2022-11-14
    相关资源
    最近更新 更多