Drupal 7 - How to assign a variable to a template?
So..I have created a module called "moon". In the module, i use moon_menu to assign a menu that calls back moon_page to display something to the browser.
function moon_page(){
$moonsvariable = 'hi this is a function';
return theme('moon_display',$moonsvariable);
}
is it possible to assign a custom variable to the template then 开发者_运维知识库use it in the template?
I would like to it in the following way.
function moon_page(){
$custom_variable = "this is a custom variable";
$moonsvariable = 'hi this is a function';
return theme('moon_display',$moonsvariable);
}
Then I like to use <? print $custom_variable ?>
in my theme to display it.
I tried variable_set
, but it does not work.
/*
* 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)
}
精彩评论