Drupal 主题模板知识

Common Core Templates 

html.tpl.php modules/system  Prints the structure of the HTML document, including the contents of <head> tags, e.g. $scripts, and $styles, as well as opening and closing <body> tags with $page_top, $page and $page_bottom regions printed inside. Unless you need to change the DOCTYPE, there’s probably no reason to override this file. 

page.tpl.php modules/system  Prints the page level regions and other hard-coded variables such as $logo, $site_name, $tabs, $main_menu, etc. Full control of the site layout is possible by manipulating this file,  and most themes provide their own version of  it. 

region.tpl.php modules/system  Prints the HTML markup for regions. 

block.tpl.php modules/block  Prints the HTML markup for blocks. 

node.tpl.php modules/node  Prints the HTML markup for nodes. 

comment.tpl.php modules/comment  Prints the HTML markup for comments. 

field.tpl.php *  modules/field/theme  Prints the HTML markup for fields. There are many different types of fields, and since this file needs to cover every case, its implementation is very general. If having semantic markup is important to you, you’ll probably end up with a few versions of this template. 

 field.tpl.php is used only when overridden by a theme. The one in modules/field/theme is only provided as a base for your work. 


Overriding Template Files 

Drupal提供了非常方便的方式来修改主题文件,我们可以使用module dev模块来找到我们需要修改的主题文件,复制到你的主题文件夹中就可以了,也可以提供下面的方式实现:
1.  Find the original template file by browsing through code or checking http://api.drupal.org 
2.  Copy and paste it into your theme directory. 
3.  Clear the site cache and reload! 

After following these three steps, Drupal will begin using the theme’s version of the file, and you are free to make whatever changes you wish. It’s that simple. 

■ Tip  A quick way to ensure that Drupal is using the template file you’ve just overridden in your theme is to add text to the top of the template file, like “Hello World.” If your text appears when you reload, you’ll know you’re working with the correct file. 

Global Template Variables 

$is_admin  Helper variable that equals TRUE if the currently logged in user is an administrator, and FALSE otherwise. 
$logged_in  Helper variable that equals TRUE if the current user is logged in, and FALSE 
otherwise. 也可以是使用$user->uid,因为没有注册用户的id始终是0
$is_front  Helper variable that uses the drupal_is_front_page() function to determine 
if the current page is the front page of the site. Equals TRUE on the front page 
(unless the database is offline), and FALSE otherwise. 
$directory  The directory in which the template being used is located. 
$user  An object that contains account information of the currently logged in user. It may be accessed by adding the line global $user; to the template you are working in. Never print any properties of it directly because it contains raw user data and thus it is insecure. 在模板文件中直接使用$user是不安全的,我们尽量使用theme('username'); for example, theme('username', array('account' => $user)). 
$language  An object that contains information about the language currently being used on the site, such as $language->dir, which contains the text direction, and $language->language which would contain en for English. It may be accessed by adding the line global $language; to the template you are working in. 

$theme_hook_suggestions  An array containing other possible theme hooks, which can be used as variants for naming template files and theme functions or to determine context. 后面会单独讨论。

$title_prefix and $title_suffix Render arrays containing elements, such as contextual links, to be printed before and after the title in templates or at the top and bottom of template files where a title does not exist. 

HTML Attributes 

$attributes  Contains HTML attributes provided by modules (mainly RDF), except for the class attribute, which is handled separately (see below). $attributes, available as $attributes_array in preprocess, is usually reserved for the top-level HTML wrapper element, such as <body> or outermost <div> in other template files. 
$classes  Contains HTML classes for templates. Usually reserved for the top-level HTML 
wrapper element, such as <body> or outermost <div> in other template files. 
$title_attributes  Contains classes for the top-level heading, such as a node or block title, of the 
template file, which is usually an <h2> for node teaser or block content. 
$content_attributes  Contains classes for the content wrapper <div>, or post body of templates. An example of how these variables are used can be found in the node.tpl.php file. 

看看block.tpl.php模板的代码

<?php 
/** 
* @file 
* Default theme implementation to display a block. 
* 
* Available variables: 
* - $block->subject: Block title. 
* - $content: Block content. 
* - $block->module: Module that generated the block. 
* - $block->delta: An ID for the block, unique within each module. 
* - $block->region: The block region embedding the current block. 
* - $classes: String of classes that can be used to style contextually through 
*   CSS. It can be manipulated through the variable $classes_array from 
*   preprocess functions. The default values can be one or more of the following: 
*   - block: The current template type, i.e., "theming hook". 
*   - block-[module]: The module generating the block. For example, the user module 
*     is responsible for handling the default user navigation block. In that case 
*     the class would be "block-user". 
* - $title_prefix (array): 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_suffix (array): An array containing additional output populated by 
*   modules, intended to be displayed after the main title tag that appears in 
*   the template. 
* 
* Helper variables: 
* - $classes_array: Array of html class attribute values. It is flattened 
*   into a string within the variable $classes. 
* - $block_zebra: Outputs 'odd' and 'even' dependent on each block region. 
* - $zebra: Same output as $block_zebra but independent of any block region. 
* - $block_id: Counter dependent on each block region. 
* - $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. 
* - $block_html_id: A valid HTML ID and guaranteed unique. 
* 
* @see template_preprocess() 
* @see template_preprocess_block() 
* @see template_process() 
*/ 
?> 
<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print 
$attributes; ?>> 
  <?php print render($title_prefix); ?> 
<?php if ($block->subject): ?> 
  <h2<?php print $title_attributes; ?>><?php print $block->subject ?></h2> 
<?php endif;?> 
  <?php print render($title_suffix); ?> 
  <div class="content"<?php print $content_attributes; ?>> 
    <?php print $content ?> 
  </div> 
</div>



At the top of the file there is a @file block, which briefly describes the purpose of the file. 
Underneath, there is a long list of variables, some of which are printed in the template file and some that are not. There are also @see references to applicable preprocess and process functions, which are discussed in more detail in the next chapter. 



你可能感兴趣的:(drupal,主题模板)