【问题标题】:Twig: replace arbitrary token in variable with template includeTwig:用模板包含替换变量中的任意标记
【发布时间】:2012-03-15 02:40:18
【问题描述】:

我正在寻找从控制器传递给我的模板的变量中的标记替换为另一个模板的包含;概念上类似于:

// controller
$post->content = "<p>Here's a short post that an enduser wrote. The enduser wants
    product info to appear here: %product_info_here%. Over and out.</p>";

// Twig template
<div class="content">
  {{ post.content
     | replace({ '%product_info_here%': include('product_info.html.twig') }) }}
</div>

从开始查看 Twig 代码开始,过滤器似乎无法访问其上下文,因此无法执行此操作。此功能是否已经存在,如果不存在,在 twig 体系结构中添加它的正确位置是什么?

更新:

这是为了使最终用户能够确定应用应在何处注入部分。为了简洁起见,我将示例放在控制器中;实际上,文字标记将成为最终用户输入的数据库中 Post 记录的一部分,并且帖子内容将传递给 Twig 模板。我希望 Twig 模板解析令牌的帖子内容,并注入部分内容。

【问题讨论】:

    标签: replace symfony template-engine twig


    【解决方案1】:

    您能否详细解释一下为什么要将模板放入控制器中?我认为以下方法也可以:

    // Twig template
    <div class="content">
      <p>Here's a short post. Product info: {% include('product_info.html.twig') %}</p>
    </div>
    

    【讨论】:

    • 好的。我不知道直接的方法,但您应该能够使用嵌入式控制器将 post.content 作为参数传递。
    猜你喜欢
    • 2023-04-04
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多