You are here

6.1 html.tpl.php

admin 的头像
Submitted by admin on 星期二, 2015-07-14 09:34

作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com

我们来看一下这个模板文件:

/**

 * @file

 * Default theme implementation to display the basic html structure of a single

 * Drupal page.

 *

 * Variables:

 * - $css: An array of CSS files for the current page.

 * - $language: (object) The language the site is being displayed in.

 *   $language->language contains its textual representation.

 *   $language->dir contains the language direction. It will either be 'ltr' or 'rtl'.

 * - $rdf_namespaces: All the RDF namespace prefixes used in the HTML document.

 * - $grddl_profile: A GRDDL profile allowing agents to extract the RDF data.

 * - $head_title: A modified version of the page title, for use in the TITLE

 *   tag.

 * - $head_title_array: (array) An associative array containing the string parts

 *   that were used to generate the $head_title variable, already prepared to be

 *   output as TITLE tag. The key/value pairs may contain one or more of the

 *   following, depending on conditions:

 *   - title: The title of the current page, if any.

 *   - name: The name of the site.

 *   - slogan: The slogan of the site, if any, and if there is no title.

 * - $head: Markup for the HEAD section (including meta tags, keyword tags, and

 *   so on).

 * - $styles: Style tags necessary to import all CSS files for the page.

 * - $scripts: Script tags necessary to load the JavaScript files and settings

 *   for the page.

 * - $page_top: Initial markup from any modules that have altered the

 *   page. This variable should always be output first, before all other dynamic

 *   content.

 * - $page: The rendered page content.

 * - $page_bottom: Final closing markup from any modules that have altered the

 *   page. This variable should always be output last, after all other dynamic

 *   content.

 * - $classes String of classes that can be used to style contextually through

 *   CSS.

 *

 * @see template_preprocess()

 * @see template_preprocess_html()

 * @see template_process()

 *

 * @ingroup themeable

 */

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"

  "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" version="XHTML+RDFa 1.0" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>

 

<head profile="<?php print $grddl_profile; ?>">

  <?php print $head; ?>

  <title><?php print $head_title; ?></title>

  <?php print $styles; ?>

  <?php print $scripts; ?>

</head>

<body class="<?php print $classes; ?>" <?php print $attributes;?>>

  <div id="skip-link">

    <a href="#main-content" class="element-invisible element-focusable"><?php print t('Skip to main content'); ?></a>

  </div>

  <?php print $page_top; ?>

  <?php print $page; ?>

  <?php print $page_bottom; ?>

</body>

</html>

在这个模板文件的上面,是注释,核心自带的模板文件里面,都有注释的,注释里面给出了当前模板文件里面可用的变量;在注释的结尾处,给出了预处理函数、处理函数,我们这里使用的变量都是在这些预处理函数、处理函数里面定义的。

我们看到,html.tpl.php模板文件里面,主要负责head里面变量的输出,里面包含$head$head_title$styles$scripts,后面的两个变量,分别负责样式、脚本的输出,我们在info文件里面定义的stylesheetsscripts就是通过这两个变量输出的。而在body里面,主要是输出$page,其它都是起辅助作用的。

Drupal6里面,这些变量内容,其实是放在page.tpl.php里面,由于每个页面的这部分内容,基本上是比较固定的,所在Drupal7里面,就将其独立成一个单独的文件了,这样page.tpl.php里面的内容,就少了很多。

template_preprocesstemplate_process里面定义的变量,在所有的模板文件里面都可以使用。在里面主要定义了下面四个变量:

$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';

  $variables['id'] = $count[$hook]++;

 

  // Tell all templates where they are located.

  $variables['directory'] = path_to_theme();

 

  // Initialize html class attribute for the current hook.

  $variables['classes_array'] = array(drupal_html_class($hook));

此外,还有一些默认变量是通过_template_preprocess_default_variables定义,这些变量大致包括:

$variables = array(

    'attributes_array' => array(),

    'title_attributes_array' => array(),

    'content_attributes_array' => array(),

    'title_prefix' => array(),

    'title_suffix' => array(),

    'user' => $user,

    'db_is_active' => !defined('MAINTENANCE_MODE'),

    'is_admin' => FALSE,

    'logged_in' => FALSE,

  );

 

$variables['is_front'] = drupal_is_front_page();

template_process里面,也可以定义变量,不过这些变量,都是直接来源于预处理函书中的,比如:

$variables['classes'] = implode(' ', $variables['classes_array']);

处理函数中的classes变量,就是直接根源于预处理函数中的classes_array,只不过将它从数组的形式转成了字符串。

 

template_preprocess_html定义了一些变量:

$variables['classes_array'][] = $variables['is_front'] ? 'front' : 'not-front';

$variables['rdf_namespaces'] = drupal_get_rdf_namespaces();

$variables['grddl_profile'] = 'http://www.w3.org/1999/xhtml/vocab';

$variables['language'] = $GLOBALS['language'];

$variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';

$variables['head_title_array'] = $head_title;

$variables['head_title'] = implode(' | ', $head_title);

$variables['theme_hook_suggestions'] = $suggestions;

 

我们注意到,好几个变量都没有在这里定义,此时我们可以尝试Google一下template_process_html,在api.drupal.org找到这个函数的定义,里面就有我们在模板里面用到的变量:

  // Render page_top and page_bottom into top level variables.

  $variables['page_top'] = drupal_render($variables['page']['page_top']);

  $variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);

  // Place the rendered HTML for the page body into a top level variable.

  $variables['page'] = $variables['page']['#children'];

  $variables['page_bottom'] .= drupal_get_js('footer');

 

  $variables['head'] = drupal_get_html_head();

  $variables['css'] = drupal_add_css();

  $variables['styles'] = drupal_get_css();

  $variables['scripts'] = drupal_get_js();

我们在这里,只需要知道变量是在预处理函数、处理函数里面定义的即可,用到的时候再去查找相应的函数。还有一个问题,Drupal是怎么把模板文件和对应的变量合并成HTML片段的,经常有人问这个问题,这时通过theme_render_template函数完成的,我们在第4集的时候,简单的介绍过这个函数。

我们在html.tpl.php里面使用这么两句话输出CSSJS文件:

  <?php print $styles; ?>

  <?php print $scripts; ?>

在实际的项目中,我们还经常这样用:

  <?php print $styles; ?>

  <link href="<?php print base_path().path_to_theme(); ?>/css/mycss.css" rel="stylesheet" type="text/css" />

  <?php print $scripts; ?>

  <script src="<?php print base_path().path_to_theme(); ?>/js/jquery-1.4a2.min.js" type="text/javascript"></script>

我们直接在模板文件里面输出了CSSJS文件,而不是通过主题的info文件,这不是什么Drupal高手推荐的方式,但是却是在实际项目当中,用的最多的一种方式。另外,就是这里的base_path().path_to_theme(),这两个函数,我们在后面还会用到。我们这里也可以使用$base_path.$directory,效果是一样的,不过我更喜欢直接使用函数的形式。注意,变量的形式,只能用于模板文件中,而函数的形式,适用的范围更广泛一些。



Drupal版本: