Drupal Articles » How to use Contemplate module


By David - Posted on 07 August 2008

Set up Contemplate module

Note: page under heavy construction, do NOT use right now

Before proceeding, ensure that you have successfully installed the Contemplate module.

The cleverly-named Contemplate module is a very helpful tool to assist you in theming the more detailed aspects of the HTML code for content (nodes), which can sometimes be "trapped" inside of a single PHP variable in your theme. One common example of this is the <?php print $submitted; ?> variable, found in a theme's node.tpl.php file. For any given piece of content, this line prints out both the date/time the content was created and the name of the user who created it (such as August 7, 2008 - 10:19am — Username depending on how you have configured your "Date format" at admin/settings/date-time). However what do you do if you want to show "only" the date? Only the author's name? Keep both the date and the user, but split them up onto different lines? Show some additional text or data on this line? This is where Contemplate module comes in to save the day.

Contemplate allows you to "break apart" these variables into smaller pieces that you can customize and theme individually, add to them, or throw away certain parts you don't want. It provides a relatively simple (albeit cluttered) interface for you to "pick and choose" what you wish to include, to then simply copy and paste into your own template, replacing the original variable (e.g. $submitted in the case of the example) with your own custom version.

Important tip about using Contemplate

One important tip about Contemplate module is that many Drupal admins recommend the use of Contemplate as tool to help you find, copy, and paste the necessary customization code into your own theme's node.tpl.php file (or other theme files). Contemplate will "allow" you to make and save changes to your theme, saving the code for those changes directly in the database... however it is widely recommended that you do not ever press Contemplate's "Submit" button to save changes. Copy Contemplate's suggested code into your own .tpl.php files (where the code works equally well), keeping all of your theming code safely in actual files (otherwise accidental uninstallation of Contemplate module, among other things, could instantly and permanently wipe out your custom templates). Another reason it's recommended to stick with all theming code in actual files is so that all theme-related code stays collected together in one place, which will help keep things simple, clear, and easier to understand for you (and others who might work on the site down the line)... it will be easier to see how the theme works (versus having to remember to edit "this" part of the theme in the files, but for "this bit and that other bit" you refer to the Contemplate page in Drupal, etc).

After you've finished using Contemplate to look up and copy/paste the code you need, simply close or leave its page without ever pressing the "Submit" button. If you find this tip confusing, don't worry - you will see the whole process in action later in this section of the guide.

Settings if you use a WYSIWYG editor

Contemplate module's interface works best if you disable any WYSIWYG editors from displaying on the Contemplate setting pages.

Using Contemplate

  1. Go to Administer > Content management > Content templates and click the "create template" link in the row labeled with your Content Type name (admin/content/templates). Expand the "Body" fieldset, and click the checkbox for Affect body output.
  2. Expand the "Body Variables" fieldset. Scroll through the list until you come to .......
  3. Open node-your_content_type.tpl.php in your favorite code editor. Locate, select, and then delete the following line of code, leaving all of the surrounding HTML intact (the span tags):
    <?php print $submitted; ?>
  4. Locate and select the line <?php print $content ?> and then delete it. This line is responsible for generating the "default" display of the data that makes up a given page (node), and is what will be replaced with custom fields provided by Contemplate module.