By admin |

搞了一天的drupal自定义模板,终于有点眉目了,赶紧记录下来,以免时间长了忘记了。

以下内容都是写在module文件中的

首先,要定义个hook_theme
[php]
function user_age_theme(){
return array(
'myprofile' => array(
'arguments' => array('uid'=>1),
'template' => 'myprofile',
),
);
}
[/php]

如上,89 'template' => 'myprofile',就可以使用自己的myprofile.tpl.php文件了。

问题来了,profile.tpl.php里面有变量,变量哪里来呢?请看下面。

93 function user_age_preprocess($var,$uid){
94 global $user;
95 $uid = $user->uid;
96 $var['profile'] = content_profile_load('profile',$uid);
97 }

不错,变量在hook_preprocess里面定义

[php]$var['profile'] = content_profile_load('profile',$uid);[/php]中间的profile就是变量名。

上面两步做完了,建个profile.tpl.php文件

然后<?php dsm($profile);?>看看吧