在树枝模板中渲染php变量

时间:2018-09-26 13:13:49

标签: php function variables twig render

我有一个像这样的php变量:

$text = "Hello {{ name }}!";

我调用$ twig-> render,将变量“ name”设置为John Doe。

在我的树枝模板中,我使用自己的功能来显示文本:

{% block test %}
  My name is: {{name}}
  <h3>
    {{ d("text") }}
  </h3>
{% endblock %}

我只是将{{name}}放在模板中以演示结果。 Twig_Function“ d”只返回$ text,所以我得到:

My name is: John Doe
<h3>
  Hello {{ name }}!
</h3>

我的预期结果应该是:

My name is: John Doe
<h3>
  Hello John Doe!
</h3>

两者都应呈现:我的普通{{name}}和我的{{name}}放在$ text中。因此,基本上,我希望树枝的渲染引擎将变量$ text放入模板后,将其渲染。有可能吗?

1 个答案:

答案 0 :(得分:1)

感谢@Patrick Q ...我只需要在template_from_string()周围放置一个include()即可,它按预期工作:

{% block test %}
  My name is: {{name}}
  <h3>
    {{ include(template_from_string(d("text"))) }}
  </h3>
{% endblock %}

以下是说明:https://twig.symfony.com/doc/2.x/functions/template_from_string.html

相关问题