额外区块类型 (EBT) - 全新的布局构建器体验❗
GLightbox is a pure javascript lightbox (Colorbox alternative without jQuery)❗
It can display images, iframes, inline content and videos with optional autoplay for YouTube, Vimeo and even self-hosted videos.
滚动
包含模板的一部分
02/10/2025, by Ivan
Menu
- 理解 Drupal
- Drupal 系统要求
- 安装 Drupal 8
- 更新 Drupal 8
- Drupal 8 网站管理
- Drupal 8 模块
- Drupal 模块比较
- Drupal 主题
- Drupal 字段类型
- 配置管理
- Drupal 多站点
- 无障碍功能
- Drupal 8 समस्या निवारण
- 为 Drupal 创建模块
- Drupal 8 API
-
Drupal 8 主题化
- 使用 .info.yml 文件定义主题
- Drupal 8 主题文件夹结构
- 在主题中添加区域
- 在 Drupal 8 主题中添加样式表 (CSS) 和 JavaScript (JS)
- Drupal 8 中的 Twig
- 子主题
- 子主题:使用 Classy 作为基础主题
- Classy 主题的 CSS 选择器
- 在你的主题中包含默认的图片样式
- 包含模板的一部分
- 在模板中使用属性
- 预处理和在 .theme 文件中修改属性
- 在 Drupal 8 中使用断点 (breakpoints)
- 创建高级主题设置
- Drupal 6、7 和 8 之间的主题差异
- 将类从 7.x 升级到 8.x
- Subtheme Inheritance
- 为自定义主题创建自动化工具 (Gulpjs)
- Drupal Twig 转换指南(tpl.php 到 html.twig)
- Drupal 8 中的 Z-Index
- 升级到 Drupal 8
许多开发者更喜欢将页眉 / 页脚的代码保存在单独的文件中,并在 page.html.twig 中调用该文件。
过程
假设你在主题文件夹中为页眉创建了以下文件:
THEME_NAME/templates/includes/header.html.twig
现在你想在以下文件中包含它:
page.html.twig
推荐方法
Drupal 8 主题的正确方法是使用 Twig 命名空间来声明当前主题的 "templates" 目录。示例如下:
{% include '@THEME_NAME/includes/header.html.twig' %}
下面的示例来自 Drupal.org 上的一个实际主题,名为 Architect。
“@architect” 指的是工作主题 (architect) 的 /templates 目录。
{% include '@architect/includes/header.html.twig' %}
Twig 命名空间在 Drupal 8 中会在安装主题时自动创建,并指向主题的 /templates 目录。基本上,在 Twig 包含语句中写 “@theme_name”(如上所示),将会引用服务器上 “your_site.com/themes/theme_name/templates” 的位置。
不推荐的方法
一种可能(但不推荐)的方法是使用以下代码来包含该文件。
{# NOT recommended #}
{% include directory ~ '/templates/includes/header.html.twig' %}
上面的方式可能会工作,但在使用子主题时会导致服务器错误。
扩展命名空间功能
组件库模块 为 Drupal 8 中的 twig 模板提供了更灵活、更复杂的组织方式,它可以与 twig "embed" 一起使用。