如何将变量从自定义模块传递到其模板文件?


8

我需要知道将变量从定制模块传递到其模板文件的最简单方法。我已经创建了custom.module并将custom.tpl.php放置在module文件夹中。

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_page() {

    $setVar = 'this is custom module';
    return theme('custom', $setVar);    
}

我添加了主题功能,但无法正常工作,有人可以建议我这段代码有什么问题吗

function theme_custom($arg) {
  return $arg['output'];
}

function custom_theme() {
  return array(
    'Bluemarine' => array(
        'variables' => 'output',
        'template' => 'Bluemarine',
     ),
  );
}

Answers:


6

与要为其编写模块的Drupal版本无关,代码中存在两个错误:

  • 您将“ Bluemarine”定义为主题函数,然后调用theme('custom'),这将调用“ custom”主题函数
  • 如果您将“ custom”定义为使用模板文件的主题函数,则theme_custom()永远不会调用

如果您正在为Drupal 6编写代码,则该代码应与以下代码相似。我假设主题函数的名称是custom。

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_theme() {
  return array(
    'custom' => array(
      'arguments' => array('output' => NULL),
      'template' => 'custom',
     ),
  );
}

function custom_page() {
    $output = 'This is a custom module';
    return theme('custom', $output);    
}

function theme_custom($output) {
}

如果您的模块实现了模板文件,则可以访问$output,以及模板中设置的任何变量template_preprocess_custom()。

例如,您可以实现类似于以下代码的代码:

function template_preprocess_custom(&$variables) {
  if ($variables['output'] == 'This is a custom module') {
    $variables['append'] = ' and I wrote it myself.";
  }
}

使用此代码,模板文件可以访问$output和$append。

作为使用模板文件的主题函数的示例,您可以看一下在node_theme()中定义的theme_node(),它使用node.tpl.php作为模板文件。由Node模块为该主题功能实现的预处理功能是template_preprocess_node()。


感谢Kiam,显然错过了OP关于使用D6的第一条评论。+1
— Laxman13年

谢谢Kiam,template_preprocess_custom(&$ variables)
— 帮了大忙

现在我要将tpl移到主题文件夹,该怎么办?
— Kamran Akhter

@KamranAkhter这是一个不同的问题。:-)
— kiamlaluno

3

您正在调用错误的主题功能。代替function theme_custom它应该是function theme_Bluemarine。您还需要将数组传递给hook_theme()变量块。在这里看到一个简单的例子。

使用您的示例(将模板和主题功能更改为custom):

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_page() {
  $setVar = 'this is custom module';
  return theme('custom', array('output' => $setVar));
}

function custom_theme() {
  $path = drupal_get_path('module', 'custom');
  return array(
    'custom' => array(
        'variables' => array('output' => null),
        'template' => 'custom',
     ),
  );
}

现在在custom.tpl.php中只需要 <?php print $output; ?>


感谢您的答复,您给的示例是节点模块,我有非节点模块。我只需要一种将变量传递到tpl文件的方法,即时消息使用drupal 6.plz帮助
— Kamran Akhter

感谢您的答复,它给了我错误没有这样的文件或目录,实际上是我期望模块文件夹中的文件custom.tpl.php已经存在,因此代码bluemarine将被自定义替换。Bluemarine是我正在使用的drupal主题。
— 卡姆兰·阿赫特

更改了上面的代码,并给了我白色的空白屏幕,我应该怎么做
— Kamran Akhter

我更改了代码,并使其在我的网站上正常工作。
— Laxman13 2011年

我只是复制粘贴上面的代码,但它不起作用
— Kamran Akhter
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.