Using multiple templates [Drupal CMS]

Source: Internet
Author: User
Tags configuration settings contact form drupal

This is the third article in the series of packpub Drupal theming articles from their new book Drupal 6 themes-Using multiple templates in Drupal 6.

Most advanced sites built today employ multiple page templates. In this section, we will look at the most common scenarios and how to address them with a phptemplate theme.

While there are running good reasons for running multiple page templates, you shocould not create additional templates solely for the purpose of disabling regions to hide blocks. while the approach will work, it will result in a performance hit for the site, as the system will still produce the blocks, only to then wind up not displaying them for the pages. the better practice is to control your block visibility.

Using a separate admin theme

With the arrival of Drupal 5, one of the most common Drupal user requests was satisfied; that is, the ability to easily designate a separate admin theme. in Drupal, designating a separate Theme for your admin interface remains a simple matter that you can handle directly from within the Admin System.

To designate a separate Theme for your admin section, follow these steps:

  1. Log in and access your site's admin system.
  2. GoAdminister | site Configuration | administration theme.
  3. Select the theme you desire from the drop-down box listing all the installed themes.
  4. ClickSave Configuration, And your selected theme shoshould appear immediately.
Multiple page or section templates

In contrast to the complete setting of setting up a separate administration theme is the comparative difficulty of setting up multiple templates for different pages or sections.

The bad news is that there is no Admin System shortcut cut-you must manually create the varous templates and customize them to suit your needs. the good news is that creating and implementing additional templates is not difficult and it is possible to attain a high degree of granularity with the techniques described below. indeed, shocould you be so inclined, you cocould literally define a distinct template for each individual page of your site.

Drupal employs an order of precedence based on a naming convention (or "Suggestions" as they are now being called on the Drupal site ). you can unlock the granularity of the system through proper application of the naming convention. it is possible, for example, to associate templates with every element on the path, or with specific users, or with a special functionality-all through the simple process of creating a new template and naming it appropriately.

The system will search for alternative templates, preferring the specific to the general, and failing to find a more specific template, will apply the defaultPage. TPL. php. Consider the following example of the order of precedence and the naming convention in action.

The custom templates above cocould be used to override the default page. TPL. PHP and theme either an entire node (page-node.tpl.php), or simply the node with an ID of 1 (page-node-1.tpl.php), or the node in edit mode (page-node-edit.tpl.php ), depending on the name given the template.

In the example above, the page-node templates wocould be applied to the node in full page view. in contrast, shocould you wish to theme the node in its entirety, you wocould need to intercept and override the default node. TPL. PHP.

The fundamental methodology of the system is to use the first template file it finds and ignore other, more general templates (if any ). this basic principle, combined with proper naming of the templates, gives you control over the template used in varous situations.

The default suggestions provided by the Drupal system shoshould be sufficient for the vast majority of theme developers. however, if you find that you need additional suggestions beyond those provided by the system, it is possible to extend your site and add new suggestions. see http://drupal.org/node/223440 for a discussion of this advanced Drupal theming technique.

Let's take a series of four examples to show how this feature can be used to provide solutions to common problems:

  1. Create a unique homepage template.
  2. Use a different template for a group of pages.
  3. Assign a specific template to a specific page.
  4. Designate a specific template for a specific user.
Create a unique homepage Template

Let's assume that you wish to set up a unique template for the homepage of a site. employing separate templates for the homepage and the Interior pages is one of the most common requests web developers hear.

With Drupal, you can, without having to create a new template, achieve some variety within a theme by controlling the visibility of blocks on the homepage. if that simple technique does not give you enough flexibility, you will need to consider using a dedicated template that is purpose-built for your homepage content.

The easiest way to set up a distinct front page template is to copy the existing page. TPL. PHP file, rename it, and make your changes to the new file. alternatively, you can create a new file from scratch. in either situation, your front-page-specific template must be named page-front.tpl.php. the system will automatically display your new file for the site's homepage, and use the default page. TPL. PHP for the rest of the site.

Note that the page-front.tpl.php is whatever page you specify as the site's front page via the site configuration settings. to override the default homepage setting visit administer | site Configuration | site information, then enter the URL you desire into the field labeled default home page.

Use a different template for a group of pages

Next, let's associate a template with a group of pages. you can provide a template to be used by any distinct group of pages, using as your guide the path for the pages. for example, to theme all the user pages you wocould create the template page-user.tpl.php.

To Theme according to the type of content, you can associate your page template with a specific node, for example, all blog entry pages can be controlled by the filepage-blog-tpl.php. the table below presents a list of suggestions you can employ to theme varous pages associated with the default functionalities in the Drupal system.

Phptemplate suggestions
Suggestion Affected page
Page-user.tpl.php User pages.
Page-blog.tpl.php Blog pages (but not the individual node pages ).
Page-forum.tpl.php Forum pages (but not the individual node pages ).
Page-book.tpl.php Book pages (but not the individual node pages ).
Page-contact.tpl.php Contact form (but not the form content ).
Assign a specific template to a specific page

Taking this to its extreme, you can associate a specific template with a specific page. by way of example, assume we wish to provide a unique template for a specific content item. let's assume our example Page is located at http://www.demosite.com/node/2/edit. the path of this specific page gives you a number of options. we cocould theme this page with any of the following templates (in addition to the default page. TPL. PHP ):

  • Page-node.tpl.php
  • Page-node-2.tpl.php
  • Page-node-edit.tpl.php

A Note on templates and URLs
Drupal bases the template order of precedence on the default path generated by the system. if the site is using a module like pathauto, which alters the path that appears to site visitors, remember that your templates will still be displayed based on the original paths. the exception here being page-front.tpl.php, which will be applied to whatever page you specify as the site's front page via the site configuration settings (Administer | site Configuration | site information).

Designate a specific template for a specific user

Assume that you want to add a personalized theme for the user with the ID of 1 (the Drupal equivalent of a super administrator ). to do this, copy the existing page. TPL. PHP file, rename it to reflect its association with the specific user, and make any changes to the new file. to associate the new template file with the user, name the file: page-user-1.tpl.

Now, when user 1 logs into the site, they will be presented with this template. Only user 1 will see this template and only when he or she is logged in and visiting the Account page.

The official Drupal site has des a collection of snippets relating to the creation of temm templates for user profile pages. the discussion is instructive and worth review, though you shoshould always be a bit cautious with user-submitted code snippets as they are not official releases from the Drupal Association. see http://drupal.org/node/35728

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.