如何在表达式语法中调用变量(Object)的方法? [Twig Templating]

时间:2013-12-22 18:32:19

标签: php symfony twig

如何在twig诱惑系统中调用表达式语法中的变量方法。 见下面的例子

{{ myObj.someMethod() }} {# this print the output of this method #}

我不想要上面的代码,因为它的打印输出方法。 但我想要这个

{% myObj.someMethod() %}

但它给了我错误

Unknown tag name "myObj" in "
{% myObj.someMethod() %}" at line 2

即使有上面​​的错误方法也被调用。

1 个答案:

答案 0 :(得分:3)

在twig中,语法{{ some variable}}会将结果打印到需要设置变量的变量中,然后使用你想要的地方

{% set myvar = myObj.someMethod() %}  /* this will store the result returned from function */

{{ myvar }} /* this will print the result in myvar */
相关问题