Drupal 7 - 如何将变量分配给模板? [英] Drupal 7 - How to assign a variable to a template?

查看:29
本文介绍了Drupal 7 - 如何将变量分配给模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以..我创建了一个名为moon"的模块.在模块中,我使用moon_menu 分配一个菜单,该菜单回调moon_page 以向浏览器显示某些内容.

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?

我想通过以下方式.

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.

我尝试了variable_set,但它不起作用.

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

这篇关于Drupal 7 - 如何将变量分配给模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆