Drupal hook_theme 使用经验
(2011-06-14 10:28:28)在开发的时候不免要使用到drupal theme 定义。
简单的例子:
<?php
function modulename_theme() { //开始定义自己的theme 使用api hook_theme
return array( //返回theme 数组
'hot_news' => array( // 给定义的theme 定义一个名称
'arguments' => array('title' => NULL, 'teaser' => NULL, 'link' => NULL), //这些都是要传递的参数,具体是在使用 theme('hot_news',arg1,arg2,arg3),这时使用到。
'template' => 'hot_news', //模板名称,它会自动搜索hot_news.tpl.php模板文件
'file' => 'get_page.inc', //这个是定义相关函数的文件,根据需要自行定义。
'path' =>drupal_get_path('module', 'modulename'),
//得到文件路径,如果theme('hot_news',arg)在template.php里面使用,需要告诉drupal具体位置,
//如果不定义,且如果在template使用,它只能在template.php同目录下查找。默认和主题同目录。
//如果在模块里使用的话,好像不需要指定 'path' 也是默认和主题同目录
),
);}
?>每个参数都会写入变量里。 variables.,比如:$variables['title'], $variables['teaser'] and $variables['link'].
接下去就可以使用:
<?php
$output = theme('hot_news', '这是标题','haha,teaser','yes, 这是link');//使用这个时候,他会输出定义的hot_news.tlp.php模板内容样式。、。
?>
还有一个功能就是预处理机制。
<?php
function template_preprocess_hot_news(&$variables) {
// $variables['title'] 的值可以使用 $title 在你的hot_news.tpl.php里面输出
$variables['title'] = '在处理一次,让它显示别的title';
$variables['teaser'] = 'strng......';
$variables['link'] = l(eeeee, 'node/'.1);
}
?>
理解hook_theme,就可以自己随心所欲来定制自己的theme。感觉到drupal的强大和灵活了。
总结:我想当告知drupal使用theme('hook',arg)时, 它需要找到hook_theme的定义,如果没有preprocess,那直接把参数送给你tpl.php文件里。如果有,它就把 theme('hook',arg)的来参数,传递给preprocess里面,可以直接用$variables['arg']得到值,看看没有重新赋 值,如果有,那就使用新的$variables['arg'],最后输出到tpl.php里面。
来自 http://blog.sina.com.cn/s/blog_5a8b8eb80100rlpd.htmlfunction book_theme() {
return array(
'book_navigation' => array(
'arguments' => array('book_link' => NULL),
'template' => 'book-navigation',
),
'book_export_html' => array(
'arguments' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL),
'template' => 'book-export-html',
),
'book_admin_table' => array(
'arguments' => array('form' => NULL),
),
'book_title_link' => array(
'arguments' => array('link' => NULL),
),
'book_all_books_block' => array(
'arguments' => array('book_menus' => array()),
'template' => 'book-all-books-block',
),
'book_node_export_html' => array(
'arguments' => array('node' => NULL, 'children' => NULL),
'template' => 'book-node-export-html',
),
//假如没有参数 当 theme('getLaoshiKeCheng')的时候 应该就是调用 theme_getLaoshiKeCheng(函数) // 'getLaoshiKeCheng' => array(
// 'arguments' => array(),
// ), );
}
这个是.........................book模块 的 theme的例子
默认是调用函数 theme_名字
如theme_book_navigation,theme_book_export_html,
theme_book_admin_table等...........................................
<?php
function product_theme() {
return array(
‘featured_product_form’ => array(‘arguments’ => array(’form’ => NULL),),
);
}
function cortisol_theme() {
$themes = array(
'survey_form' => array(
'arguments' => array('form'),
'function' => 'theme_survey_form',
),
);
return( $themes );
}
?>
下面三段是 直接使用函数 不使用tpl.php的用法function views_bulk_operations_theme() { $themes = array(
'views_node_selector' => array(
'arguments' => array('element' => NULL),
),
'views_bulk_operations_confirmation' => array(
'arguments' => array('objects' => NULL, 'invert' => FALSE, 'view' => NULL),
),
);
case VBO_STEP_CONFIRM: $operation = $form_state['storage']['operation'];
$query = drupal_query_string_encode($_GET, array('q'));
$form = confirm_form($form,
t('Are you sure you want to perform %operation on the selected items?', array('%operation' => $operation['label'])),
array('path' => $_GET['q'], 'query' => $query),
theme('views_bulk_operations_confirmation', $form_state['storage']['selection'], $form_state['storage']['selectall'], $plugin->view)
);
break;
function theme_views_bulk_operations_confirmation($objects, $invert, $view) {
$selectall = $invert ? (count($objects) == 0) : (count($objects) == $view->total_rows);
if ($selectall) {
$output = format_plural(
$view->total_rows,
'You selected the only item in this view.',
'You selected all <strong>@count</strong> items in this view.'
);
}
}
hook_theme 里面的参数function怎么没用
| 1 2 3 4 5 6 7 8 9 10 | function test_theme() {
$theme_path = drupal_get_path( 'theme' , 'test' );
return array (
'web_game' => array ( 'function' => 'theme_web_game' , 'template' => 'web_game' , 'path' => $theme_path . '/templates' ),
);
}
function theme_web_game( $variables )
{
die (print_r( "1231321" ));
}
|
为什么进不了theme_web_game这个方法。。。难道不是这么用的? |
| |
1 个回答
来自
http://www.drupalla.com/node/1911覆写用户登录区块user_block,drupal theme template.php 里设置
- sites/default/files/languages/zh-hans_40e00289b049f7d8ee44211a930fe5a6.js目录 尚未成功设置,/tmp/filevzFYtX文件无法上传。
- sites/default/files/languages/zh-hans_40e00289b049f7d8ee44211a930fe5a6.js目录 尚未成功设置,/tmp/fileqazhYL文件无法上传。
星期一, 07/20/2009 - 21:47 — admin
默认主题,在后台区块里面可以看到一个用户登录的区块,但是有时想改掉它的表现形式,按照自己的设计来设计。 这时就需要修改user_block。这个用户登录的block,注意这是区块和user 登录注册那个不一样。这个用户登录区块是在user模块里面定义的。可以先看看代码: 文件位于 user.module 分析源文件: url($_GET['q'], array('query' => drupal_get_destination())), '#id' => 'user-login-form', '#validate' => user_login_default_validators(),//用到 user_login_default_validators验证函数 '#submit' => array('user_login_submit'), ); $form['name'] = array('#type' => 'textfield', //定义用户名表单 '#title' => t('Username'), '#maxlength' => USERNAME_MAX_LENGTH, '#size' => 15, '#required' => TRUE, ); $form['pass'] = array('#type' => 'password',//定义密码表单 '#title' => t('Password'), '#maxlength' => 60, '#size' => 15, '#required' => TRUE, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Log in'), ); $items = array();//返回items 数组 主要是两个衔接 if (variable_get('user_register', 1)) { $items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));// 创建新帐号衔接 } $items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));//创建找回密码衔接 $form['links'] = array('#value' => theme('item_list', $items)); return $form; } ?> 定义一个user 登录区块 uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {//判断是否登录,而且是否是注册页面,第一个参数不是user 并且 第二个参数不是数字 $block['subject'] = t('User login'); $block['content'] = drupal_get_form('user_login_block');// 区块内容就是返回一个form,调用上面的form 数组,drupal_get_form 这个函数强大,很好用。 } return $block; ?> 理解上面的源文件,开始做覆写的工作,主要参考以下代码: 下面就考虑如何在drupal theme template.php里面覆写这个区块了。 第一步:建立template.php文件 在所启用的主题下面新建立一个template.php 的文件夹,然后把下面的代码拷贝进去,把yourthemename 改为自己所用的主题名称。 不要拷贝"?>" function yourthemename_theme() { return array( 'user_login_block' => array( 'template' => 'user_login', 'arguments' => array('form' => NULL), ), ); } function yourthemename_preprocess_user_login_block(&$variables) { //去掉两个衔接。 $variables['form']['name']['#title']='请填写你的大名'; $variables['form']['pass']['#title']='请出示的暗号'; $variables['form']['submit']['#value']='快快登录'; $variables['form']['links']['#value']=''; } //拷贝到这里,后面问号就不要拷贝了?> 第二步:建立user_login.tpl.php文件 放在主题里面。 把下面代码拷贝进去即可: 直接在 user_login.tpl.php里面
效果如下图:

星期六, 09/12/2009 - 15:59 — admin
form id样式 是什么意思? 它不是隐藏的变量吗?这个登录框的区块变量,任意组合。定义CSS。
我另一个站点使用的 修改代码,右边效果, 体检网 http://jstjw.com/
template.php 代码
<?php
function yourthemename_theme() {
return array(
'user_login_block' => array(
'template' => 'user_login',
'arguments' => array('form' => NULL),
),
);
}
function yourthemename_preprocess_user_login_block(&$variables) {
//注释代码,你需要就可以去掉
// $variables['form']['name']['#value'] ='请输入用户名';
// $variables['form']['pass']['#value'] ='请输入密码';、
// $variables['form']['name']['#attributes']['OnClick'] = 'this.value=""';
// $variables['form']['pass']['#attributes']['OnClick'] = 'this.value=""';
// $variables['form']['name']['#title']='用户名';
// $variables['form']['action']['#title']='';
//$variables['form']['submit']['#value']='登录';
//$variables['form']['links']['#value']='';
$variables['form']['name']['#title']='';
$variables['form']['pass']['#title']='';
}
?>
然后直接在user_login.tpl.php ,样式自己调整。
<table border="0" width="200px" cellpadding="0" cellspacing="0" height="100px">
<tr><td >用户名:</td><td align="left"><?php print drupal_render($variables['form']['name']);?></td></tr>
<tr><td>密码:</td><td align="left"><?php print drupal_render($variables['form']['pass']);?></td></tr>
<tr><td align="right"><?php print drupal_render($variables['form']['submit']);?><?php print drupal_render($variables['form']['form_id']);?><?php print drupal_render($variables['form']['form_build_id']);?></td><td align="center"><a href="/user/register">注册帐号</a></td></tr>
</table>
来自 http://hellodrupal.info/node/79
drupal7 指定模板theme()的用法
(2013-08-13 15:52:51)如何在drupal模块中自定义html页面呢?
下面是一个我写的例子:
首先在module页面里写hook_menu() 方法:
function test_menu(){
$items['test/about']=array(
'title'=>t('About'),
'description'=>t('about us'),
'page callback'=>'test_about', //回调函数
'access callback'=>TRUE,
'type'=>MENU_NORMAL_ITEM,
);
}然后写hook_theme() 函数,指定html页面
function test_theme(){
return array(
'test_about'=>array(
'template'=>'about',//指向about.tpl.php文件
),
'test_contact'=>array( //
'template'=>'contact',
),
);
}最后我们在回调函数test_about 中调用在test_theme()中指定的about模板:
function test_about(){
return theme('about');
}//带参数的回调函数
function test_about(){
global $base_url;
return theme('about',array('base_url',$base_url,'aa'=>'ssss'));
}这些参数可以在指定的about.tpl.php文件中调用。
完成以上三步,您已经成功为test模块下的about 文件,指定了一个自定义的html模块。
来自
http://blog.sina.com.cn/s/blog_7d85d15a0101gg8z.html
这样输出登陆框会有form
这样输出登陆框会有form id样式,影响设想的页面布局,该怎么办呢?