本模块输出 [英] Theming Module Output
问题描述
我想要做的是在模块中生成一些原始输出。
What I am trying to do is generate some raw output within a module.
我想将一组数据传递到一个模板文件,然后使用该数据填充模板中的代码。模板由我的主题文件夹中的文件表示。
I would like to pass an array of data through to a template file, and then use that data to populate the code from the template. The template is represented by a file in my theme folder.
我为某个URL(/ itunes)设置了一个钩子:
I have a hook set up for a certain URL (/itunes):
$items['itunes'] = array(
'page callback' => 'itunespromo_buildpage',
'type' => MENU_SUGGESTED_ITEM,
'access arguments' => array('access content'),
);
.. itunespromo_buildpage ...
..inside itunespromo_buildpage...
function itunespromo_buildpage() {
//grab some data to pass through to template file, put into $promo_data
$details = theme('itunes_page', array(
'promo_data' => $promo_data,
));
return $details;
}
这是hook_theme():
Here is the hook_theme():
function itunespromo_theme() {
return array(
'itunes_page' => array(
'template' => 'itunes_page',
),
);
}
在我的主题的template.php内:
Inside my theme's template.php:
function geddystyle_itunes_page($vars) {
return print_r($vars['promo_data'], true);
}
现在,$ promo_data 正在传递罚款,print_r'd到结果页。然而,我想要这个$ promo_data变量,并使用它在我的itunes_page.tpl.php模板文件。
Right now, $promo_data is being passed through fine, and it is print_r'd on to the result page. However, I'd like to then take this $promo_data variable and use it in my itunes_page.tpl.php template file.
我确定我是在这附近我应该调用某种渲染函数并将$ promo_data变量从函数itunespromo_theme()传递给它?
I'm kind of certain I'm close here. Am I supposed to call some sort of render function and pass the $promo_data variable to it from function itunespromo_theme()?
推荐答案
我相信你只需要更新你的hook_theme()来提供发送变量到你的模板文件的能力。
I believe you just need to update your hook_theme() to provide the ability to send variables to your template file.
这样的东西应该是诀窍:
Something like this should do the trick:
function itunespromo_theme($existing, $type, $theme, $path) {
return array(
'itunes_page' => array(
'variables' => array(
'promo_data' => NULL,
),
'template' => 'itunes_page',
)
);
}
此外,而不是直接调用theme()函数,实际上正在构建一个可渲染的数组,并让Drupal调用theme()函数。你应该做的是调用drupal_render,这反过来又为你调用了theme()。在这里看一下这个建议一点点清楚一点:
Also, instead of calling the theme() function directly what you want to be doing is actually constructing a renderable array and letting Drupal call the theme() function. What you should be doing is calling drupal_render which in turn calls theme() for you. Look at this piece of advice here for a little more clarity:
http://drupal.org/node/1351674#comment-5288046
在您的情况下,您将更改您的功能itunespromo_buildpage来查看如下所示:
In your case you would change your function itunespromo_buildpage to look something like this:
function itunespromo_buildpage() {
//grab some data to pass through to template file, put into $promo_data
$output = array(
'#theme' => 'itunes_page',
'#promo_data' => $promo_data //call $promo_data from the tpl.php page to access the variable
);
$details = drupal_render($output);
return $details;
}
这篇关于本模块输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!