One big topic in Drupal: theme

What is a theme function?

A theme function is a PHP function within Drupal that is prefixedwith theme_. Theme functions display HTML, and which can becustomized by themes.

All functions that produce HTML for display should be themeable.This means that they should be named with the theme_ prefix, andinvoked using theme() rather than being called directly. Thisallows themes to override the display of any Drupal object.


How to use theme function?

Every chunk of output that is themed through the theme() functionis called a theme hook. There are two ways to provide a defaultimplementation. The easier way is to provide a function, and therecommended way is to provide a template and a correspondingpreprocessor function.

A simple hook_theme implementation might look like this:

function forum_theme() {
  return array(
      'forums'=> array(
          'template' => 'forums',
          'variables' => array('forums' =>NULL, 'topics' => NULL, 'parents' =>NULL, 'tid' => NULL, 'sortby' =>NULL, 'forum_per_page' => NULL),
      ),
//...
  );
}


It also tells us that the forums theme function takes 6 variables(or arguments), and they all default to NULL. (All arguments mustbe given defaults as we have no way to assure that atheme('forums', ...) call will provide the proper information. Ifin doubt, make the default NULL). These arguments are translatedinto the named variables for the template. When calling this themehook, an author might write:


  $output = theme('forums', array('forums'=> $forums, 'topics' => $topics,'parents' => $parents, 'tid' => 17,'sortby' => 'ASC', 'forums_per_page'=> 25))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值