我想设置一些全局变量,我需要在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
我定义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
。我怎么能这样做?
答案 0 :(得分:0)
function themename_preprocess(&$variables) {
// code
}
清除缓存后,对我有用吗?