【发布时间】:2017-07-18 13:53:08
【问题描述】:
我想设置几个全局变量,我需要在 Drupal 8 的所有主题的 Twig 模板中使用这些变量。
Drupal 7 documentation 提到预处理功能:
themeName_preprocess 这个以主题本身命名。适用于所有挂钩。
所以我将以下函数添加到我的themename.theme 文件中,但未设置变量。
function themename_preprocess(&$variables) {
$theme_path = $variables['base_path'] . $variables['directory'];
$variables['theme_path'] = $theme_path;
$variables['images_path'] = $theme_path . "/images/";
$variables['templates_path'] = $theme_path . "/templates/";
}
当我定义themename_preprocess_page(如下)而不是定义themename_preprocess 时,变量已正确定义并可在page.html.twig 模板中使用。
function themename_preprocess_page(&$variables) {
$theme_path = $variables['base_path'] . $variables['directory'];
$variables['theme_path'] = $theme_path;
$variables['images_path'] = $theme_path . "/images/";
$variables['templates_path'] = $theme_path . "/templates/";
}
但我希望变量在 all 模板中可用,而不仅仅是 page.html.twig。我该怎么做?
【问题讨论】:
-
原来的解决方案是简单地重建缓存。定义和更改
themename_preprocess_page()在没有重建缓存的情况下工作,这只是奇怪和误导。
标签: drupal drupal-8 drupal-templates drupal-preprocess