php - Twig_Error_Loader : Template is not defined - Drupal

标签 php drupal twig drupal-8

我在 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 的文件。

enter image description here

我做错了什么?我在 MAMP 和 OSX,El Capitan 中运行 Drupal。 Drupal 8.3.7

最佳答案

在 Drupal 8 中使用 Twig 的 includeextends 时,您必须指定 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/

相关文章:

php - 弃用函数 : require_once()

Twig - 将字符串数据附加到同一变量

php - 如何使用 TwigBridge 在 Laravel 中使用自定义 Twig 函数

php在循环内搜索数组

php - 复制并修改数组

php - mysql - 在一个查询中获取显式值和随机值

php - MySQL查询按列排名

html - 如何在 CSS 中引用带空格的长类名?

php - 无法打开所需的database.inc(尝试使用drupal)

javascript - 用一种形式自定义整个 symfony 网站的 UI