Drupal 7-从hook_theme获取变量 [英] Drupal 7 - get variables from hook_theme
问题描述
我试图将变量从自定义模块传递到tpl文件。
在我的自定义模块(命名示例)中
I tried to pass a variable from a custom module to a tpl file. In my custom module (named example)
1。我通过hook_menu创建了一个带有参数的路由:
function example_menu() {
$items['example/fancybox-photos/%'] = array(
'page callback' => 'example_display_fancybox_photos',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
2。我创建了页面回调函数:
function example_display_fancybox_photos($nid) {
$nodePhoto = node_load($nid);
$field_photo = field_get_items('node', $nodePhoto, 'field_photo');
$photo = [
"field_photo" => $field_photo[0]['uri'],
....
];
return theme('example_fancybox_photos', array('infosPhoto' => $photos));
}
3。我创建了一个hook_theme
function example_theme() {
$themes = array();
$themes['example_fancybox_photos'] = array(
'template' => 'templates/example-fancybox-photos',
'variables' => array('infosPhoto' => NULL),
);
return $themes;
}
4。我终于在模板文件夹(位于主题文件夹中)中创建了一个名为 example-fancybox-photos.tpl.php的tpl
<pre><?php print var_dump($infosPhoto); ?></pre>
结果为NULL
我做到了一些研究,但我不明白为什么变量仍然为NULL。
I did some researchs but i dont understand why the variable is still NULL.
感谢您的帮助!
推荐答案
您已通过 $ photos
,但您将数组设置为 $ photo
。尝试更改
You have passed $photos
but you have array as $photo
. try to change that
希望以下代码对您有所帮助。
Hope the below code helps you.
function example_menu(){
$items['example/fancybox-photos/%'] = array(
'page callback' => 'example_display_fancybox_photos',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
function example_display_fancybox_photos($nid){
$photos = 'value from example module!';
return theme('example_fancybox_photos',array('photos' => $photos));
}
function example_theme() {
$path = drupal_get_path('module', 'example');
return array(
'example_fancybox_photos' => array(
'variables' => array('photos' => null),
'template' => 'example_fancybox_photos',
'path' => $path,
),
);
}
将tpl文件example_fancybox_photos.tpl.php放在模块目录中并放在其中使用以下代码。
Place your tpl file example_fancybox_photos.tpl.php in your module directory and inside it use the below code.
<?php print $photos; ?>
或
function example_theme() {
return array(
'example_fancybox_photos' => array(
'variables' => array('photos' => null),
'template' => 'example_fancybox_photos',
),
);
}
将tpl文件example_fancybox_photos.tpl.php放在主题目录中并放在其中放置以下代码
Place your tpl file example_fancybox_photos.tpl.php in your theme directory and inside it place the below code
<?php print $photos; ?>
这篇关于Drupal 7-从hook_theme获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!