我想为我的自定义模块制作一个树枝模板,输出下一篇和上一篇文章链接。
在我的 .module 文件中,我有
<?php
/**
* @file
* Code for the nextprev module.
*/
function nextprev_theme($existing, $type, $theme, $path) {
return [
'nextprev_template' => [
'variables'=> [
'nextprev' => 'Some_value',
],
],
];
}
在我的控制器文件中,我使用
public function build() {
/**
* {@inheritdoc}
*/
$node = \Drupal::request()->attributes->get('node');
$created_time = $node->getCreatedTime();
$nextprevlinks .= $this->generateNext($created_time);
$nextprevlinks .= $this->generatePrevious($created_time);
$renderable = [
'#theme' => 'nextprev_template',
'#nextprev' => 'nextprevlinks',
];
$rendered = drupal_render($renderable);
}
}
我想将我的 $nextprevlinks 在 twig 中打印为 {{ nextprev }}
我在我的模块文件夹中制作了树枝模板并且它可以工作,但是我无法打印出我的 {{ nextprev }} 变量,当我使用 kint 时它返回 Null。
我还添加了 nextprev.routing.yml :
nextprev.block:
path: /node
defaults:
_controller: Drupal\nextprev\Controller\NextPrevLinksBlock::build
requirements:
_permission: 'access content'