所以..我创建了一个名为“月亮”的模块。在模块中,我使用moon_menu分配一个菜单,该菜单回调moon_page以向浏览器显示内容。
function moon_page(){
$moonsvariable = 'hi this is a function';
return theme('moon_display',$moonsvariable);
}
是否可以为模板分配自定义变量,然后在模板中使用它?
我希望以下列方式:
function moon_page(){
$custom_variable = "this is a custom variable";
$moonsvariable = 'hi this is a function';
return theme('moon_display',$moonsvariable);
}
然后我想在我的主题中使用<? print $custom_variable ?>
来显示它。
我尝试了variable_set
,但它不起作用。
答案 0 :(得分:3)
/*
* Implementation of hook_theme().
*/
function moon_theme($existing, $type, $theme, $path){
return array(
'moon' => array(
'variables' => array('content' => NULL),
'file' => 'moon', // place you file in 'theme' folder of you module folder
'path' => drupal_get_path('module', 'moon') .'/theme'
)
);
}
function moon_page(){
// some code to generate $content variable as array
$content['data1'] = 'Lorem ipsum';
$content['data2'] = 'Ipsum lorem';
return theme('moon', $content); // use $content variable in moon.tpl.php template
// Or you can use few 'variables' in hook_theme function
// smth like this 'variables' => array('content1' => NULL, 'content2' => NULL)
// and return page as theme('moon', var1, var2)
}