【问题标题】:use } into smarty string使用 } 进入 smarty 字符串
【发布时间】:2011-07-15 03:37:59
【问题描述】:

我试图在 smarty 模板字符串中使用键符号“}”,但它会产生错误。问题只出现在“}”而不是“{”。 我需要打印:

var naciones = [{label:'Country', value:'1'}, {label:'Country', value:'2'}];

解决方案:

var naciones = [{/literal}{foreach from=$paises item=pa}{literal}{label:"{/literal}{$pa->getNacionalidad()}{literal}", value:"{/literal}{$pa->getId()}{literal}"},{/literal}{/foreach}{literal}];

示例:

{literal}
<script type="text/javascript">
var naciones = [{/literal}
{foreach from=$paises item=pa}
{'{label:"'|cat:$pa->getNacionalidad()|cat:'", value:"'|cat:$pa->getId()|cat:'"'}{cat:'"}, '}{/foreach}{literal}];
$('#nacionalidad-ac').autocomplete({
  source:naciones,
  change: function(event, ui){
    $('#nacionalidad').val(ui.item.value);
  }
});
</script>
{/literal}

谢谢

【问题讨论】:

    标签: templates smarty


    【解决方案1】:

    您需要{literal} {/literal} 标签才能正确转义 Smarty 模板中的花括号。对于带有大括号的内联 Javascript 也是必需的。

    【讨论】:

    • 最好避免内联Javascript。
    • 谢谢,我写道:var naciones = [{/literal}{foreach from=$paises item=pa}{literal}{label:"{/literal}{$pa->getNacionalidad() }{literal}", value:"{/literal}{$pa->getId()}{literal}"},{/literal}{/foreach}{literal}];
    【解决方案2】:

    您可以使用 {literal} 停止解析。因此,带有“{”的 smarty 模板看起来像

    This is just a text with a {$smartyString}
    There is also an {literal} } {/literal} sign in here that could give you some trouble.
    

    【讨论】:

    • 当然。我使用文字标签,但在我的示例中,打开/关闭标签的组合很奇怪,我不知道如何应用该原则。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多