Commit 43d73f81 authored by Gábor Hojtsy's avatar Gábor Hojtsy

#166071 by jjeff and dvessel: move default template preprocess code to...

#166071 by jjeff and dvessel: move default template preprocess code to template_preprocess, and add *lots* of documentation to template files and functions
parent 42be9d13
......@@ -270,9 +270,7 @@ function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
}
}
// It would be too much of a performance hit to let every module have
// a generic preprocess function; themes and theme engines can do that.
if ($type != 'module' && function_exists($prefix .'_preprocess')) {
if (function_exists($prefix .'_preprocess')) {
$info['preprocess functions'][] = $prefix .'_preprocess';
}
if (function_exists($prefix .'_preprocess_'. $hook)) {
......@@ -1591,9 +1589,45 @@ function _theme_table_cell($cell, $header = FALSE) {
}
/**
* Prepare the variables passed to the page.tpl.php template. Uses the arg()
* function to generate a series of page template files suggestions based on
* the current path.
* Adds a default set of helper variables for preprocess functions and
* templates. This comes in before any other preprocess function which makes
* it possible to be used in default theme implementations (non-overriden
* theme functions).
*/
function template_preprocess(&$variables, $hook) {
global $user;
static $count = array();
// Track run count for each hook to provide zebra striping.
// See "template_preprocess_block()" which provides the same feature for sidebar blocks.
$count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
$variables['id'] = $count[$hook]++;
// Tell all templates where they are located.
$variables['directory'] = path_to_theme();
// Flag front page status.
$variables['is_front'] = drupal_is_front_page();
// Tell all templates by which kind of user they're viewed.
$variables['logged_in'] = ($user->uid > 0);
$variables['is_admin'] = user_access('access administration pages');
}
/**
* Process variables for page.tpl.php
*
* Most themes utilize their own copy of page.tpl.php. The default is located
* inside "modules/system/page.tpl.php". Look in there for the full list of
* variables.
*
* Uses the arg() function to generate a series of page template suggestions
* based on the current path.
*
* The $variables array contains the following arguments:
* - $content
* - $show_blocks
*
* @see page.tpl.php
*/
function template_preprocess_page(&$variables) {
/* Set title and breadcrumb to declared values */
......@@ -1744,9 +1778,19 @@ function template_preprocess_page(&$variables) {
}
}
/*
* Prepare the values passed to the theme_node function to be passed
* into standard template files.
/**
* Process variables for node.tpl.php
*
* Most themes utilize their own copy of node.tpl.php. The default is located
* inside "modules/node/node.tpl.php". Look in there for the full list of
* variables.
*
* The $variables array contains the following arguments:
* - $node
* - $teaser
* - $page
*
* @see node.tpl.php
*/
function template_preprocess_node(&$variables) {
$node = $variables['node'];
......@@ -1786,15 +1830,26 @@ function template_preprocess_node(&$variables) {
$variables['submitted'] = '';
$variables['picture'] = '';
}
// Clean up name so there are no underscores.
$variables['template_files'][] = 'node-'. $node->type;
}
/**
* Process variables for block.tpl.php
*
* Prepare the values passed to the theme_block function to be passed
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
* block.tpl.php is used.
*
* Most themes utilize their own copy of page.tpl.php. The default is located
* inside "modules/system/block.tpl.php". Look in there for the full list of
* variables.
*
* The $variables array contains the following arguments:
* - $block
*
* @see block.tpl.php
*/
function template_preprocess_block(&$variables) {
global $sidebar_indicator;
......
<?php
// $Id$
/**
* @file node.tpl.php
*
* Theme implementation to display a node.
*
* Available variables:
* - $title: the (sanitized) title of the node.
* - $content: Node body or teaser depending on $teaser flag.
* - $picture: The authors picture of the node output from
* theme_user_picture().
* - $date: Formatted creation date (use $created to reformat with
* format_date()).
* - $links: Themed links like "Read more", "Add new comment", etc. output
* from theme_links().
* - $name: Themed username of node author output from theme_user().
* - $node_url: Direct url of the current node.
* - $terms: the themed list of taxonomy term links output from theme_links().
* - $submitted: themed submission information output from
* theme_node_submitted().
*
* Other variables:
* - $node: Full node object. Contains data that may not be safe.
* - $type: Node type, i.e. story, page, blog, etc.
* - $comment_count: Number of comments attached to the node.
* - $uid: User ID of the node author.
* - $created: Time the node was published formatted in Unix timestamp.
* - $zebra: Outputs either "even" or "odd". Useful for zebra striping in
* teaser listings.
* - $id: Position of the node. Increments each time it's output.
*
* Node status variables:
* - $teaser: Flag for the teaser state.
* - $page: Flag for the full page state.
* - $promote: Flag for front page promotion state.
* - $sticky: Flags for sticky post setting.
* - $status: Flag for published status.
* - $comment: State of comment settings for the node.
* - $readmore: Flags true if the teaser content of the node cannot hold the
* main body content.
* - $is_front: Flags true when presented in the front page.
* - $logged_in: Flags true when the current user is a logged-in member.
* - $is_admin: Flags true when the current user is an administrator.
*
* @see template_preprocess()
* @see template_preprocess_node()
*/
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block">
<?php print $picture ?>
<?php if ($page == 0): ?>
<?php if (!$page): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
......@@ -23,10 +70,5 @@
<?php print $content ?>
</div>
<?php
if ($links) {
print $links;
}
?>
<?php print $links; ?>
</div>
\ No newline at end of file
<?php
// $Id$
/**
* @file block.tpl.php
*
* Theme implementation to display a block.
*
* Available variables:
* - $block->subject: Block title.
* - $block->content: Block content.
* - $block->module: Module that generated the block.
* - $block->delta: This is a numeric id connected to each module.
* - $block->region: The block region embedding the current block.
*
* Helper variables:
* - $block_zebra: Outputs 'odd' and 'even' dependent on the default sidebar
* block regions.
* - $zebra: Same output as $block_zebra but independent of any block region.
* - $block_id: counter within the default sidebar block regions.
* - $id: Same output as $block_id but independent of any block region.
* - $is_front: Flags true when presented in the front page.
* - $logged_in: Flags true when the current user is a logged-in member.
* - $is_admin: Flags true when the current user is an administrator.
*
* @see template_preprocess()
* @see template_preprocess_block()
*/
?>
<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?>">
<?php if ($block->subject): ?>
<h2><?php print $block->subject ?></h2>
<?php endif;?>
<div class="content"><?php print $block->content ?></div>
<div class="content">
<?php print $block->content ?>
</div>
</div>
......@@ -14,7 +14,7 @@
* - $css: An array of CSS files for the current page.
* - $directory: The directory the theme is located in, e.g. themes/garland or
* themes/garland/minelli.
* - $is_front: TRUE if the current page is the front page.
* - $is_front: TRUE if the current page is the front page. Used to toggle the mission statement.
* - $logged_in: TRUE if the user is registered and signed in.
* - $is_admin: TRUE if the user has permission to access administration pages.
*
......@@ -30,8 +30,6 @@
* - $body_classes: A set of CSS classes for the BODY tag. This contains flags
* indicating the current layout (multiple columns, single column), the current
* path, whether the user is logged in, and so on.
* - $is_front: True if the front page is currently being displayed. Used to
* toggle the mission.
*
* Site identity:
* - $logo: The path to the logo image, as defined in theme configuration.
......@@ -69,8 +67,8 @@
* - $closure: Final closing markup from any modules that have altered the page.
* This variable should always be output last, after all other dynamic content.
*
* @see template_preprocess()
* @see template_preprocess_page()
* @see phptemplate_engine_preprocess()
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
......
......@@ -25,29 +25,3 @@ function phptemplate_theme($existing, $type, $theme, $path) {
return $templates;
}
/**
* Adds additional helper variables to all templates.
*
* Counts how many times certain hooks have been called. Sidebar left / right are special cases.
*
* @param $variables
* A series of key-value value pairs.
* @param $hook
* The name of the theme function being executed.
*/
function phptemplate_engine_preprocess(&$variables, $hook) {
global $user;
static $count = array();
// Create variables so anything which is themed can be zebra striped automatically.
$count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
$variables['id'] = $count[$hook]++;
// Tell all templates where they are located.
$variables['directory'] = path_to_theme();
$variables['is_front'] = drupal_is_front_page();
// Tell all templates by which kind of user they're viewed.
$variables['logged_in'] = ($user->uid > 0);
$variables['is_admin'] = user_access('access administration pages');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment