11

我正在尝试在 Drupal 7 中构建自己的模块。

所以我创建了一个名为“moon”的简单模块

function moon_menu() {
  $items = array();
      $items['moon'] = array(
      'title' => '',
      'description' => t('Detalle de un Programa'),
      'page callback' => 'moon_page',
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
  );

  return $items;
}

function moon_page(){


$id = 3;

$content = 'aa';

}

在moon_page() 函数中,我喜欢从我的主题文件中加载一个自定义模板“moon.tpl.php”。

这可能吗?

4

5 回答 5

15
/*
 * 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

  return theme('moon', $content); // use $content variable in moon.tpl.php template
}
于 2011-03-15T03:12:21.690 回答
10

对于您自己的东西(不覆盖来自另一个模块的模板)?

当然,您只需要:

$args 是一个数组,其中包含由 hook_theme() 实现指定的模板参数。

于 2011-03-14T22:11:05.857 回答
4

对于 Drupal 7,它对我不起作用。我替换了 hook_theme 中的行

'file' => 'moon', by 'template' => 'moon' 

现在它对我有用。

于 2012-10-22T12:37:27.563 回答
3

在 drupal 7 中,使用时出现以下错误:

return theme('moon', $content);

导致“致命错误:第 1071 行的 drupal_install\includes\theme.inc 中不支持的操作数类型”

这是使用修复的:

theme('moon', array('content' => $content));

于 2013-02-12T05:38:26.290 回答
0

你可以使用moon_menu和hook_theme

<?php

/**
 * Implementation of hook_menu().
 */
function os_menu() {
  $items['vars'] = array(
    'title' => 'desc information',
    'page callback' => '_moon_page',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

function _moon_page() {    
  $fields = [];
  $fields['vars'] = 'var';

  return theme('os', compact('fields'));
}

/**
 * Implementation of hook_theme().
 */
function os_theme() {
  $module_path = drupal_get_path('module', 'os');

  return array(
    'os' => array(
      'template' => 'os',
      'arguments' => 'fields',
      'path' => $module_path . '/templates',
    ),
  );
}
于 2016-06-08T15:54:49.933 回答