Using Contemplate to Create Custom Page Layouts

The concept of creating a custom tpl.php file was a bit daunting to begin with, although there are a large number of tutorials and articles out there on how to create these files. A quick look around the Drupal community for community contributed “layout modules”, resulted in a few modules which reduced the complexity of this task.

The Content Templates module (or contemplate) for short is a community contributed module which is available for Drupal 6 and Drupal 7.  The module itself allows the modification of the title and body fields using administrator defined templates. These templates can then be used to create custom template files. If you were to combine this with the Content Construction Kit (CCK) module, you would have a very powerful tool for handling the theming of custom content types within your Drupal website. This article will show you how to quickly and competently create these files, and will hopefully extend the overall functionality of your website. In order to create this functionality, you will need to install Drupal (either 6 or 7) with the following modules: 

* These don’t necessarily need to be installed to provide this functionality. These were installed to increase the functionality of the Content Construction Kit (CCK).

The first thing that we need to have done is to install Drupal. Whether your installing this on a local host machine or web server, this process is not very complex. Once we Drupal installed the next step is to enable our modules. To do this, click on Site Configuration / Modules, then select the modules that are needed in order to create this functionality (see above). You will also need to administer the correct permissions for these modules (this is located under “User Management / Permissions”). Once this has been configured and you have created a custom content type (or are just using the standard “Page” or “Story” content types), you will need to go and begin editing your custom content template. The settings for this are located within your site under Administration / Content Management / Content Types / Templates.

You can create as many or as few as you wish depending on the number of content types that you have or are wanting to have on your Drupal website. Your custom defined fields should now be showing within an edit box under the content type that you are creating a template for. These can be edited to be located at different locations around the page, and there is a large number of other modifications that can be made. Once all the changes have been made, you will need to create a custom template.php file for this content type. The first step in doing so is to create a new blank text file, and copy into the contents of the node.tpl.php file (located in the theme directory of the theme that you are using on your site).

The next step is to copy over the contents of the template that you created above. This needs to sit just under the contents of the node.tpl.php file that you created in the above step. Once you have done this, you will need to name your template file to the name of your content type as it exists within your site - e.g. node-portfolio.tpl.php. From here all you will need to do is upload this custom file into your theme folder on your web server ( or copy the file into the theme directory if you have your Drupal site installed locally), and then your done.

Tags: