Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 9 THE THEME SYSTEM

The page.tpl.php File

The next template file we’ll examine is the page.tpl.php file. This template file focuses on the elements that are displayed between the <body> and </body> tags and includes the HTML structure of the page, including DIV tags and snippets of PHP code. If you look back at the html.tpl.php template, you’ll see

<?php print $page; ?> where the value of $page is the contents of page.tpl.php.

This template file defines the structure of the page as it is displayed to the user. If you look back at the example that we started this chapter with, all of the elements that we styled in the Grayscale theme were elements that are rendered by the page.tpl.php template. In the following code, you’ll see all of the elements that are rendered on the page and the conditional logic that drives what is displayed on a page and how that page is structured. Like the html.php.tpl file, the page.tpl.php file consists of a mixture of HTML and PHP snippets, where the PHP snippets include conditional logic to determine whether a value is set and displaying the values associated with several variables, where variables may contain anything from a simple value like “42” to a complex HTML structure including JavaScript. An example is the first variable contained in the page.tpl.php file, $logo. This variable holds the HTML necessary to render the site’s logo if one was supplied, which is why there’s conditional logic to test to see whether a logo was supplied.

The structure of the page.tpl.php file that resides in /modules/system is as follows:

<div id="page-wrapper"><div id="page">

<div id="header"><div class="section clearfix">

<?php if ($logo): ?>

<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home" id="logo">

<img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" /> </a>

<?php endif; ?>

<?php if ($site_name || $site_slogan): ?> <div id="name-and-slogan">

<?php if ($site_name): ?> <?php if ($title): ?>

<div id="site-name"><strong>

<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home"><span><?php print $site_name; ?></span></a>

</strong></div>

<?php else: /* Use h1 when the content title is empty */ ?> <h1 id="site-name">

<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home"><span><?php print $site_name; ?></span></a>

</h1>

<?php endif; ?> <?php endif; ?>

<?php if ($site_slogan): ?>

<div id="site-slogan"><?php print $site_slogan; ?></div> <?php endif; ?>

</div> <!-- /#name-and-slogan --> <?php endif; ?>

203

CHAPTER 9 THE THEME SYSTEM

<?php print render($page['header']); ?> </div></div> <!-- /.section, /#header -->

<?php if ($main_menu || $secondary_menu): ?> <div id="navigation"><div class="section">

<?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'clearfix')), 'heading' => t('Main menu'))); ?>

<?php print theme('links__system_secondary_menu', array('links' => $secondary_menu, 'attributes' => array('id' => 'secondary-menu', 'class' => array('links', 'clearfix')), 'heading' => t('Secondary menu'))); ?>

</div></div> <!-- /.section, /#navigation --> <?php endif; ?>

<?php if ($breadcrumb): ?>

<div id="breadcrumb"><?php print $breadcrumb; ?></div> <?php endif; ?>

<?php print $messages; ?>

<div id="main-wrapper"><div id="main" class="clearfix">

<div id="content" class="column"><div class="section">

<?php if ($page['highlighted']): ?><div id="highlighted"><?php print render($page['highlighted']); ?></div><?php endif; ?>

<a id="main-content"></a>

<?php print render($title_prefix); ?>

<?php if ($title): ?><h1 class="title" id="page-title"><?php print $title; ?></h1><?php endif; ?>

<?php print render($title_suffix); ?>

<?php if ($tabs): ?><div class="tabs"><?php print render($tabs); ?></div><?php endif; ?>

<?php print render($page['help']); ?>

<?php if ($action_links): ?><ul class="action-links"><?php print render($action_links); ?></ul><?php endif; ?>

<?php print render($page['content']); ?> <?php print $feed_icons; ?>

</div></div> <!-- /.section, /#content -->

<?php if ($page['sidebar_first']): ?>

<div id="sidebar-first" class="column sidebar"><div class="section"> <?php print render($page['sidebar_first']); ?>

</div></div> <!-- /.section, /#sidebar-first --> <?php endif; ?>

<?php if ($page['sidebar_second']): ?>

<div id="sidebar-second" class="column sidebar"><div class="section"> <?php print render($page['sidebar_second']); ?>

</div></div> <!-- /.section, /#sidebar-second --> <?php endif; ?>

</div></div> <!-- /#main, /#main-wrapper -->

204

CHAPTER 9 THE THEME SYSTEM

<div id="footer"><div class="section"> <?php print render($page['footer']); ?>

</div></div> <!-- /.section, /#footer -->

</div></div> <!-- /#page, /#page-wrapper -->

The default variables that are available to the page.tpl.php file are shown in Table 9-3.

Table 9-3. Standard Variables Available to page.tpl.php

Variable

Description of contents

 

 

$base_path

$directory

$is_front $logged_in $is_admin $front_page

$logo $site_name $site_slogan

$main_menu (array)

$secondary_menu (array)

$breadcrumb

The base URL path of the Drupal installation; at the very least, this will always default to /.

The directory the template is located in, e.g., modules/system or themes/bartik

TRUE if the current page is the front page

TRUE if the user is registered and signed in

TRUE if the user has permission to access administration pages

The URL of the front page; use this instead of $base_path when linking to the front page. This includes the language domain or prefix.

The path to the logo image, as defined in the theme’s configuration The name of the site, empty when it has been disabled in theme settings

The slogan of the site, empty when it has been disabled in theme settings

An array containing the main menu links for the site, if they have been configured

An array containing the secondary menu links for the site, if they have been configured

The breadcrumb trail for the current page

205

CHAPTER 9 THE THEME SYSTEM

Continued

Variable

Description of contents

 

 

$title_prefix

An array containing additional output populated by modules, intended

 

to be displayed in front of the main title tag that appears in the template

$title

The page title, for use in the actual HTML content

$title_suffix (array)

An array containing additional output populated by modules, intended

 

to be displayed after the main title tag that appears in the template

$message

Status and error messages that should be displayed prominently

$tabs (array)

Tabs linking to any sub-pages beneath the current page

$action_links (array)

Actions local to the page, such as “Add menu” on the menu

 

administration interface

$feed_icons

A string of all the feed icons for the current page

$node

The node object, if there is an automatically loaded node associated with

 

the page, and the node ID is the second argument in the page’s path (e.g.,

 

node/12345 and node/12345/revisions, but not comment/reply/12345).

 

 

The variables $page['help'], $page['highlighted'], $page['content'], $page['sidebar_first'], $page['sidebar_second'], $page['header'], and $page['footer'] represent regions on the page. A region represents the physical containers that a site administrator can assign any block-level element to (e.g., the logon form, the search block, a node, a view, or a menu). If you don’t specify any regions in your theme’s .info file, you get the regions just listed by default. I’ll show you how to create additional regions in the upcoming section that describes how to construct your theme’s .info file.

The region.tpl.php File

This template file focuses on how regions are displayed on your site. The default region.tpl.php file is pretty simple—essentially just displaying the content that is assigned to a region.

<?php if ($content): ?>

<div class="<?php print $classes; ?>"> <?php print $content; ?>

</div>

<?php endif; ?>

The variables available to this template file by default are as shown in Table 9-4.

206

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]