在模块中使用 hook_theme 将变量传递给 twig
Passing variables to twig using hook_theme within a module
我完全知道如何在 Drupal 7 中执行此操作,因此我将解释我通常使用 Drupal 7 执行的操作。
在制作自定义模块时我经常使用hook_theme,它非常强大且可重复使用!
/**
* Implements hook_theme().
*/
function MODULE_theme() {
$themes = array();
$themes['name_of_theme'] = array(
'path' => drupal_get_path('module', 'module') .'/templates',
'template' => 'NAME_OF_TEPLATE',
'variables' => array(
'param1' => NULL,
'param2' => NULL,
),
);
return $themes;
}
然后我会使用
来调用这个主题
theme('name_of_theme', array(
'param1' => 'VALUEA',
'param2' => 'VALUEB'
));
这会 return html 我会很高兴。
所以 Drupal 8 已经出来了,需要好好掌握它。
/**
* Implements hook_theme().
*/
function helloworld_theme() {
$theme = [];
$theme['helloworld'] = [
'variables' => [
'param_1' => [],
'param_2' => 'hello',
]
];
return $theme;
}
在我的控制器中,我正在使用
$hello_world_template = array(
'#theme' => 'helloworld',
'variables' => [
'param_1' => 'hello world',
'param_2' => 'hello from another world'
],
);
$output = drupal_render($hello_world_template,
array(
'variables' => array(
'param_1' => $param_1,
'param_2' => $param_2,
)
)
);
return [
'#type' => 'markup',
'#markup' => $output
];
我正在获取我的模板的输出,但是我不确定将我的参数传递到哪里以便它们在我的模板中可用(只是指出我的变量可用它们只是 null 作为在 hook_theme)
中定义
我也对我可能做的根本错误的想法持开放态度,如果我的方法不是最佳实践,我愿意采用替代方法。
找到问题,
改变这个,
$hello_world_template = array(
'#theme' => 'helloworld',
'variables' => [
'param_1' => 'hello world',
'param_2' => 'hello from another world'
],
);
对此,
$hello_world_template = array(
'#theme' => 'helloworld',
'#param_1' => $param_1,
'#param_2' => $param_2
);
我现在可以看到我正在传递的变量。
我是否还有更好的选择?
我完全知道如何在 Drupal 7 中执行此操作,因此我将解释我通常使用 Drupal 7 执行的操作。
在制作自定义模块时我经常使用hook_theme,它非常强大且可重复使用!
/**
* Implements hook_theme().
*/
function MODULE_theme() {
$themes = array();
$themes['name_of_theme'] = array(
'path' => drupal_get_path('module', 'module') .'/templates',
'template' => 'NAME_OF_TEPLATE',
'variables' => array(
'param1' => NULL,
'param2' => NULL,
),
);
return $themes;
}
然后我会使用
来调用这个主题theme('name_of_theme', array(
'param1' => 'VALUEA',
'param2' => 'VALUEB'
));
这会 return html 我会很高兴。
所以 Drupal 8 已经出来了,需要好好掌握它。
/**
* Implements hook_theme().
*/
function helloworld_theme() {
$theme = [];
$theme['helloworld'] = [
'variables' => [
'param_1' => [],
'param_2' => 'hello',
]
];
return $theme;
}
在我的控制器中,我正在使用
$hello_world_template = array(
'#theme' => 'helloworld',
'variables' => [
'param_1' => 'hello world',
'param_2' => 'hello from another world'
],
);
$output = drupal_render($hello_world_template,
array(
'variables' => array(
'param_1' => $param_1,
'param_2' => $param_2,
)
)
);
return [
'#type' => 'markup',
'#markup' => $output
];
我正在获取我的模板的输出,但是我不确定将我的参数传递到哪里以便它们在我的模板中可用(只是指出我的变量可用它们只是 null 作为在 hook_theme)
中定义我也对我可能做的根本错误的想法持开放态度,如果我的方法不是最佳实践,我愿意采用替代方法。
找到问题,
改变这个,
$hello_world_template = array(
'#theme' => 'helloworld',
'variables' => [
'param_1' => 'hello world',
'param_2' => 'hello from another world'
],
);
对此,
$hello_world_template = array(
'#theme' => 'helloworld',
'#param_1' => $param_1,
'#param_2' => $param_2
);
我现在可以看到我正在传递的变量。
我是否还有更好的选择?