Drupal 7-如何从模块加载模板文件?
我正在尝试用Drupal7构建自己的模块 所以我创建了一个简单的模块叫做“月亮”Drupal 7-如何从模块加载模板文件?,drupal,drupal-theming,Drupal,Drupal Theming,我正在尝试用Drupal7构建自己的模块 所以我创建了一个简单的模块叫做“月亮” function moon_menu() { $items = array(); $items['moon'] = array( 'title' => '', 'description' => t('Detalle de un Programa'), 'page callback' => 'moon_page', 'access ar
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”
这可能吗 对于您自己的东西(不重写另一个模块中的模板)
当然,您只需要:
- 将模板注册到
- 调用主题('moon',$args)
$args是一个数组,它包含hook_theme()实现指定的模板参数。对于Drupal 7,它对我不起作用。我替换了hook_主题中的行
/*
* 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
}
'file' => 'moon', by 'template' => 'moon'
现在它对我起作用了 在drupal 7中,使用时出现以下错误:
返回主题('moon',$content)代码>
导致“致命错误:第1071行drupal\u install\includes\theme.inc中不支持的操作数类型”
这是通过以下方式修复的:
主题('moon',数组('content'=>$content))代码>您可以使用带有hook主题的moon\u菜单
<?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',
),
);
}
//我刚刚放置了打印主题('moon',$content),但它显示了一个空白页面。我在这里做错了什么吗?如果在页面回调中打印某些内容,这意味着“我只希望打印这些内容。例如:文件、json、xml等”。改为使用“返回主题('moon',$args)”。您是否更新了主题注册表(重新保存主题选择表单或使用devel.module)。谢谢,这对我很有帮助。但是需要一些修改。在返回数组中的moon_theme()函数时,数组键“file”应使用“template”重新计算,否则会在Drupal 6中产生错误。在Drupal 7中,“path”工作正常,但当使用“path”调用时,该文件应确切地称为“theme/moon”,没有任何扩展名。如果使用“模板”,drupal7将搜索“theme/moon.tpl.php”。