【发布时间】:2019-10-10 20:34:44
【问题描述】:
我在 Drupal 8 中创建了一个自定义模块。我希望自定义模块显示自定义标题,而不是显示在所有其他页面上的标准标题。
我使用此处找到的文档创建了一个自定义树枝模板:https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-for-custom-module。我也以此为参考:https://www.drupal.org/forum/support/module-development-and-code-questions/2017-05-15/drupal-8-custom-module-template。
我能够创建模板,但是该模板似乎在页眉模板之后和页脚模板之前被调用和呈现。因此,使用文档我无法更改头文件中的任何内容。
关于如何为自定义模块的标题创建自定义树枝文件的任何想法?下面是我的代码的 sn-ps:
模块文件:
function calendar_theme($existing, $type, $theme, $path) {
return array(
'calendar_display' => array(
'variables' => array('test' => NULL),
'template' => 'calendar-template',
),
);
}
在我的控制器文件中:
namespace Drupal\calendar\Controller;
class CivilCController {
public function calendar(){
$content[] = [
//stuff here
];
$content[] = [
//stuff here
];
return [
'#theme' => 'calendar_display',
'#test' => $content,
];
}
}
现在我有一个名为calendar-template.html.twig 的树枝文件。但是,此文件在标题下方呈现。
【问题讨论】:
-
您必须澄清“标题”的含义。如果标题只是您主题中的一个区域并且在每个页面上都相同,我个人只会更改主题的模板文件之一,不需要模块。我可能会这样做的另一种方法是创建一个提供块的自定义模块,然后将该块放在标题区域中。
-
你还没有说你是如何把你的主题代码放到页面上的。
标签: php drupal twig drupal-8 drupal-modules