在自定义Drupal 8模块中,我无法从控制器中呈现一个模板。
我称之为控制器方法:
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom-search-engine';
return $build;}
达到控制器没有问题,我可以var_dump在里面。但是没有呈现模板的内容。
我的模块文件结构如下所示:
bretagnecom_search_engine.module源
./src:控制器
./src/Controller: DefaultController.php
/模板:bretagnecom-搜索-工程.html.twig
知道我做错了什么吗?我通常使用内联模板从控制器直接呈现几个html,但这次我想在他的模板文件中隔离我的html。
谢谢大家的帮助!
发布于 2018-11-15 18:06:26
我猜hook_theme()中没有定义模板。
首先,只需将连字符改为下划线:
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom_search_engine';
return $build;
}
在bretagnecom_search_engine.module中添加:
/**
* Implements hook_theme().
*/
function bretagnecom_search_engine_theme() {
$themes = [
'bretagnecom_search_engine' => [
'variables' => [
'your_custom_variable_1' => NULL,
'your_custom_variable_2' => NULL
]
];
如果没有变量,只需删除代码的这一部分。
您可以在这里找到更多信息:https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-for-custom-module
https://stackoverflow.com/questions/53296159
复制相似问题