Twig_Error_Loader:模板未定义 - Drupal

时间:2017-09-30 15:54:47

标签: php drupal twig drupal-8

我在Drupal中创建了一个include

{% include directory ~ '/partials/header.html.twig' %}

但是我收到了这个错误:

Twig_Error_Loader: Template "themes/custom/mytheme/partials/header.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/custom/mytheme/partials/header.html.twig" in the Drupal theme registry.) in "themes/custom/mytheme/page--front.html.twig" at line 1. in Twig_Loader_Chain->getCacheKey() (line 115 of vendor/twig/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

1 个答案:

答案 0 :(得分:4)

在Drupal 8中使用Twig的includeextends时,您必须指定theme / modules命名空间。

以下是Drupal.org的完整说明:https://www.drupal.org/node/2143557

另外,您应该将部分文件添加到自定义主题的模板文件夹中。这是一种最佳实践。

使用示例

{% include "@mytheme/partials/favicons.html.twig" %}