我在 Drupal 中创建了一个 include
:
{% include directory ~ '/partials/header.html.twig' %}
但是我收到了这个错误:
Twig_Error_Loader:模板“themes/custom/mytheme/partials/header.html.twig”未定义(Drupal\Core\Template\Loader\ThemeRegistryLoader:无法找到模板“themes/custom/mytheme/partials/header.html.twig"在 Drupal 主题注册表中。)在第 1 行的“themes/custom/mytheme/page--front.html.twig”中。在 Twig_Loader_Chain->getCacheKey() 中(vendor/twig/的第 115 行twig/lib/Twig/Loader/Chain.php).
根据 Drupal 和 Twig 文档,我的做法是正确的。
我有一个名为 partials
的目录和一个名为 header.html.twig
的文件。
我做错了什么?我在 MAMP 和 OSX,El Capitan 中运行 Drupal。 Drupal 8.3.7
最佳答案
在 Drupal 8 中使用 Twig 的 include
或 extends
时,您必须指定 theme/modules 命名空间。
这里是 Drupal.org 的完整解释:https://www.drupal.org/node/2143557
此外,您应该将部分文件 添加到自定义主题的templates 文件夹中。这是最佳做法。
使用示例
{% include "@mytheme/partials/favicons.html.twig" %}
关于php - Twig_Error_Loader : Template is not defined - Drupal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504436/