官术网_书友最值得收藏!

Understanding the anatomy of a theme

Drupal themes can consist of a multitude of files each with its own purpose, format, and syntax. This recipe will introduce each of these types with an explanation of what they do.

Getting ready

It will be useful to navigate to the Garland folder at themes/garland to browse and view the files inside a typical, fully featured theme. Garland also uses the PHPTemplate theming engine which is the most commonly used and recommended engine across Drupal's core and contributed themes.

How to do it...

The following table outlines the types of files typically found inside a theme's folder and the naming conventions to be followed for some of them.

  

Type           Mandatory?           Description
mytheme.info           Yes           Configuration file which provides information to Drupal about a theme named mytheme.
page.tpl.php           Yes           A template file which determines the layout of all Drupal pages.
node.tpl.php           No           A template file which determines the layout of a node inside a Drupal page.
block.tpl.php           No           A template file which determines the layout of a block.
*.tpl.php           No           Other template files which allow the customization and styling of themable aspects of Drupal.
style.css           No           CSS stylesheet—if this file exists, it will be automatically included in the theme.
script.js           No           Javascript file—if this file exists, it will be automatically included in the theme.
*.js           No           Other Javascript files which will need to be explicitly included in the .info file.
favicon.ico           No           Shortcut icon—if this file exists, it will be automatically included in the theme unless overridden from within Drupal.
logo.png           No           Site logo—if this file exists, it will be automatically included in the theme unless overridden from within Drupal.
screenshot.png           No           Theme preview image—if this file exists, it will be automatically included in the theme.
template.php           No           PHPTemplate master file where some of the more complicated and powerful tweaks and overrides occur.

Perusing the contents of each of the available files will prove very useful as we go along developing our theme.

How it works...

When a theme is added, Drupal first parses its .info file. This file, as its extension suggests, provides information about the theme such as its name, Drupal version compatibility, regions declared, CSS stylesheets used, JavaScript files included, and so on. In other words, Drupal uses it to find out the configuration and features of a theme.

The .info file also specifies the theming engine being used by the theme which, by default, is PHPTemplate. Theme engines allow theme developers to communicate with Drupal using a simpler and more convenient interface commonly via template files. A number of them also introduce their own language formats for use in these template files.

Template files in PHPTemplate themes are those that use the .tpl.php extension. Unlike other engines, these files just use PHP and HTML and do not rely on any special markup languages.

There's more...

Other theme engines besides PHPTemplate are also available. However, only a handful of themes in Drupal's contribution repository rely on them.

Other theme engine types

The PHPTemplate engine is the most widely prevalent theming engine used in the Drupal ecosystem and is a part of the Drupal core package. Themes using other engines such as Smarty or Xtemplate are rare and will be structured quite differently. A list of engines can be found at http://drupal.org/project/Theme+engines.

Tip

Engine-less theme

The Chameleon theme which is a part of core is a theme which does not use a templating engine and relies on straight PHP to get things done.

主站蜘蛛池模板: 武山县| 邻水| 新干县| 施甸县| 托克逊县| 安康市| 沈丘县| 南康市| 沁源县| 昌平区| 永宁县| 白玉县| 定远县| 灯塔市| 麦盖提县| 济南市| 新巴尔虎左旗| 澄江县| 洛浦县| 汕头市| 屯留县| 应城市| 荃湾区| 阜南县| 建始县| 南投市| 木兰县| 大英县| 宿迁市| 荆州市| 延川县| 高淳县| 莲花县| 石柱| 漠河县| 瑞昌市| 徐闻县| 婺源县| 郸城县| 开封县| 闽清县|