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))
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() {
//...
}
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:
本文深入探讨了Drupal中的主题函数,解释了其在显示HTML页面中的作用,并提供了使用主题函数自定义Drupal对象的实践指南。
610

被折叠的 条评论
为什么被折叠?



