【问题标题】:Drupal 8 , render Twig from string with twig functionsDrupal 8,使用 twig 函数从字符串渲染 Twig
【发布时间】:2014-09-13 07:58:25
【问题描述】:

您好,我使用 Drupal 8 树枝,它无法捕捉他的功能。

$TwigString = '<div>{{result}}{% include "some.html.twig" with {somevars:vars}%}</div>';
$Twig = new \Twig_Environment(new \Twig_Loader_String());
$Twig->setLoader(new \Twig_Loader_String());
$html = $Twig->render($TwigString,array("result" => "Success!"));

我明白了:

成功!some.html.twig

我该怎么办?

【问题讨论】:

    标签: php function twig drupal-8


    【解决方案1】:

    您可能会对 Drupal 8 中的 inline_template 功能感兴趣:https://www.drupal.org/node/2311123

    您在故障排除时遇到困难的原因是您加载了一个全新的 Twig 环境,只使用了字符串加载器。如果您只有字符串加载器,则不能包含include "some.html.twig" 之类的文件,因为您没有可用的Filesystem 加载器。

    请参阅this issue,了解我们如何改进我们在 Drupal 核心中对内联模板的字符串加载器的使用,因为字符串加载器实际上会传递所有内容,因此很难进行故障排除。

    inline_template 理论上应该允许您将字符串加载器与其他加载器结合使用。

    也作为包含/扩展/等的旁白。在撰写本文时(2015 年 3 月),您在 Drupal 8 中有几个主要选项:

    • 主题和模块模板的命名空间支持:@mymodule/file.html.twig
    • Registry loader 用于在hook_theme() 注册的模板:file.html.twig
    • 相对于根的完整路径:modules/mymodule/templates/file.html.twig

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多