模板未从控制器 drupal 8 呈现
Template not rendered from controller drupal 8
我无法从自定义 Drupal 8 模块中的控制器呈现模板。
我正在调用这个控制器方法:
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom-search-engine';
return $build;}
到达控制器没问题,我可以var_dump进去。但是模板的内容没有渲染。
我的模块文件结构如下所示:
bretagnecom_search_engine.module 源码
./src:
控制器
./src/Controller:
DefaultController.php
./模板:
bretagnecom-search-engine.html.twig
知道我做错了什么吗?我通常使用内联模板直接从控制器渲染一些 html,但这次我想在他的模板文件中隔离我的 html。
感谢大家的帮助!
我想 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
我无法从自定义 Drupal 8 模块中的控制器呈现模板。
我正在调用这个控制器方法:
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom-search-engine';
return $build;}
到达控制器没问题,我可以var_dump进去。但是模板的内容没有渲染。
我的模块文件结构如下所示:
bretagnecom_search_engine.module 源码
./src: 控制器
./src/Controller: DefaultController.php
./模板: bretagnecom-search-engine.html.twig
知道我做错了什么吗?我通常使用内联模板直接从控制器渲染一些 html,但这次我想在他的模板文件中隔离我的 html。
感谢大家的帮助!
我想 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