我想设置几个全局变量,我需要在 Drupal 8 中的所有主题的 Twig 模板中都可以使用这些变量。
Drupal 7 文档提到了预处理功能:
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
我定义themename_preprocess_page
(下面)时,变量被正确定义并在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/";
}
但我希望变量在所有模板中都可用,而不仅仅是page.html.twig
. 我怎样才能做到这一点?