主题模块输出

时间:2011-12-05 21:22:00

标签: drupal drupal-modules

我要做的是在模块中生成一些原始输出。

我想将数据数组传递给模板文件,然后使用该数据填充模板中的代码。模板由我的主题文件夹中的文件表示。

我为某个网址设置了一个钩子(/ itunes):

$items['itunes'] = array(
    'page callback'     =>  'itunespromo_buildpage',
    'type'              =>  MENU_SUGGESTED_ITEM,
    'access arguments'  =>  array('access content'),
);

..在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():

function itunespromo_theme() {
    return array(
        'itunes_page'   =>  array(
            'template'  =>  'itunes_page',
        ),
    );
}

在我的主题里面的template.php:

function geddystyle_itunes_page($vars) {
    return print_r($vars['promo_data'], true);
}

现在,$ promo_data 正在通过罚款,并且打印到结果页面。但是,我想接受这个$ promo_data变量并在我的itunes_page.tpl.php模板文件中使用它。

我确定我离这儿很近。我应该调用某种渲染函数并从函数itunespromo_theme()传递$ promo_data变量吗?

1 个答案:

答案 0 :(得分:0)

我相信你只需更新你的hook_theme()就可以将变量发送到你的模板文件。

这样的事情可以解决问题:

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,而drupal_render又为你调用theme()。在这里看一下这条建议,以便更清晰一点:

http://drupal.org/node/1351674#comment-5288046

在你的情况下,你会改变你的功能itunespromo_buildpage看起来像这样:

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;
}