全部模板引用都始於theme函數並遞歸調用其嵌套的theme函數,如下示意代碼簡單地描述了theme的整個機制php
function theme($template, $variables) { // hook_theme 聲明template foreach($modules as $module) { $theme_info += {$module}_theme(); } if(empty($theme_info[$template])) return TEMPLATE_NOT_FOUND; // preprocess function加工$variables if(function_exists('{$theme}_preprocess_{$template}')) { $variables = {$theme}_preprocess_{$template}($variables); } $info = $theme_info[$template]; // 若是設置了template屬性,將讀取tpl if(!empty($info['template'])) return get_template_from_file('{anymodule or theme path}'.$info['template'].'.tpl.php', $variables); else { // 不然將調用theme function if(function_exists('{$theme}_{$template}')) return {$theme}_{$template}($variables); else // 這是最base的模板,通常位於module內部 return theme_{$template}($variables); } }
{$template} : 當前模板名稱 {$theme} : 當前使用的theme名稱
* template必須經過hook_theme聲明 * template必須使用theme('hook', $vars);來輸出 * theme裏的template.php能夠實現HOOK,前綴必須是theme的名稱 * form在沒有指定#theme的時候,默認會引用與form ID同樣的模板,即form是user_login,它會自動調用`theme('user_login', $variables);` * preprocess functions能夠修改模板變量
user/login的page callback是function user_login,它並無聲明template,因此第一步應該在template.php聲明user_login templatehtml
function yourtheme_theme() { $items = array(); $items['user_login'] = array( 'render element' => 'form', 'path' => drupal_get_path('theme', 'yourtheme') . '/templates', 'template' => 'user-login', 'preprocess functions' => array( 'yourtheme_preprocess_user_login' ), ); return $items; }
經過以上代碼,能夠建立sites/all/themes/yourtheme/templates/user-login.tpl.php,它會自動應用在user login page上。而有時候有必須修改模板的動態數據,但form提供給template的變量固定,這時就能夠用yourtheme_preprocess_user_login對模板變量進行修改。node
https://www.drupal.org/node/1...web
theme_htmlapi
theme_page函數
theme_user_profileui
theme_regionurl
theme_blockcode
theme_nodeorm
theme_field
theme_comment
theme_taxonomy_term
theme_webform_form
theme_menu_local_task
template不單單是固定不變,template的命名能夠規則化,使template能夠指定屬於某些內容而不是所有。而這種規則化通常須要module預先實現,已經有至關一部分module實現規則化,例如node, taxomoy。
假如咱們須要指定ID = 1的內容模板,咱們能夠把template命名爲node--1.tpl.php。但咱們很難從程序文件或者代碼裏得知module支持那些命名規則,因此官方提供一篇文檔方便開發者查閱:Drupal 7 Template (Theme Hook) Suggestions
// in template.php function mytheme_menu_tree($variables) { return '<ul class="menu">' . $variables['tree'] . '</ul>'; } function mytheme_menu_link(array $variables) { $element = $variables['element']; $sub_menu = ''; if ($element['#below']) { $sub_menu = drupal_render($element['#below']); } $output = l($element['#title'], $element['#href'], $element['#localized_options']); return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n"; }
// in theme-settings.php function hook_form_system_theme_settings_alter(&$form, &$form_state, $form_id = NULL) { // Work-around for a core bug affecting admin themes. See issue #943212. if (isset($form_id)) { return; } // Create the form using Forms API: http://api.drupal.org/api/7 $form['theme_settings']['background_file'] = array( '#type' => 'managed_file', '#title' => t('Background'), '#required' => TRUE, '#upload_location' => file_default_scheme() . '://backgrounds/', '#default_value' => theme_get_setting('background_file'), '#upload_validators' => array( 'file_validate_extensions' => array('gif png jpg jpeg'), ), ); }
// in template.php function theme_preprocess_html(&$variables, $hook) { $fid = theme_get_setting('background_file'); $variables['background_url'] = ''; if($fid) { $loaded = file_load($fid); if(!$loaded->status) { $loaded->status = FILE_STATUS_PERMANENT; file_save($loaded); } $variables['background_url'] = file_create_url($loaded->uri); } }
$items = array( l('Batch history', 'admin/reports/salesforce/batch'), l('Currently queued items', 'admin/reports/salesforce/current'), ); $output .= theme('item_list', array('items' => $items));
$header = array('Nid', 'Title', 'Created'); $rows []= array(1, 'item 1', '2009/1/3'); $rows []= array(2, 'item 2', '2009/1/4'); $output = theme('table', array( 'header' => $header, 'rows' => $rows ));
$links[] = array( 'href' => 'node/1', 'title' => 'blog 1', 'query' => array('tag' => 'blog'), ); print theme('links', array( 'links' => $operations, 'attributes' => array('class' => array('links', 'inline')), ));