template.php

Switching Drupal tpl.php files at will: Old Switchy McTipplefep's Trick

Section:

One of the first lessons they teach you at the School of Drupal Arts, Arcane Sciences and Sorcery is that a tpl.php can have dynamically generated wildcards or "suggestions". You can see this every day drupal themes: you can simply use the "node.tpl.php" file if you want only one style for a node. Where as if you want different styles for three node types: blog, story & page, you create the following files:
  • node-page.tpl.php
  • node-story.tpl.php
  • node-blog.tpl.php
Obviously, this pattern exists far beyond nodes. For page.tpl.php, user/register can have its own tpl.php file:
  • page-user-register.tpl.php
Where as user/1/edit will be (yes -- these suggestions will remove numbers... i think... i was hung over that day in class):
  • page-user-edit.tpl.php
This may be fine and dandy if your agenda is either to build a simple drupal theme with a few exceptions, or to build manly Texas sized drupal themes with enough tpl.php files, and duplicate html code to choke a donkey. Old Switchy McTipplefep's trick is for anyone who:
  • wants 3 styles that are shared by 20 blocks
  • wants 4 possible layouts for dozens of pages in a site
  • has 12 node types, but wants them to share 3 styles
The magic happens in your theme's $theme_name . '_preprocess_' . $theme function. Below are examples for how a theme named "example" would do it.
function example_preprocess_node(&$vars) {
  $node = $vars['node'];
 // only switch for story type
  switch($node->type) {
    case 'story':
      $vars['template_files'][] = 'node-batman';
      break;
  }
}

function example_preprocess_block(&$vars) {
  // sure why not -- we'll pass our node.tpl.php file to our block
  // That's how tpl pimpin works sometimes
  $vars['template_files'][] = 'node-batman';
}

How to create a block region for node.tpl.php

Update: appparently, today was the day to write about this. Nedjo Rogers submitted a handbook page that shows a different method of achieving the same end.

For the most part, Drupal 4.7's block system is underutilized. This is a shame; with the proper templating, drupal's block system can become a valuable workhorse. In this tutorial, you will learn:

  1. How to override default functions in the phptemplate.engine
  2. Create new regions to place blocks
  3. Pass a block region into node.tpl.php

By the end of this tutorial, you will have the ability to place blocks into node's like a so:

Quick and Clean CCK teasers Via PHPtemplate

If you are like me, you've been pulling your hair out trying to make teasers work in CCK. Well, as it so happens, I figured out a crazy simple phptemplate method of making CCK body fields act like any other node's body. Observe:

A CSS ID for Every Menu Item

As you can see, these menu links have unique icons. Yet another miracle accomplished using Drupal's PHPTemplate. This technique is especially cool because it automatically generates CSS ID's from the menu link's name.

TEMPLATE.PHP: Override theme_menu_item() (includes/menu.inc)

<?php function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) { return _phptemplate_callback('menu_item', array( 'leaf' => $leaf, 'mid' => $mid, 'children' => $children )); } ?>

Now create a menu_item.tpl.php file.

Overriding Themeable Functions: The Where's, the Why's, and the How's

There's good news and bad news. First, the good news: overriding theme functions is easy. The bad news: every theme function is different, and there isn't a standard proceedure of going about it; if you don't know what you are doing, its quite easy to accidently do something ugly, or foolish.

So, in the next few tutorials, we are going to explore the hows, whys, why nots, and what ifs of overriding theme functions. Each of these functions will present a different set of challenges, and opprotunities to do something stupid, etc. Today's lesson is "Building a better node form". In this tutorial you will learn

Extreme Drupal Theming with PHPtemplate

Drupal's phptemplate is the most powerful, simple, and flexible templating/theming system on the planet*. Yet, it seems that the majority of drupal themers, (and wannabe drupal themers) are ignorant of its true power.

More disturbingly, vast numbers of people still insist that Joomla!, typo3, and wordpress's templating systems are easier, sexier, and slicker. These people are either loony, or wrong.

As an expert in PHPtemplate (isn't that scary), I've decided its my duty to show the true power of PHPtemplate to world.

Consider these tutorials, a proper (and steadily growing) introduction to drupal's templating system.

An Introduction: Dominating the User Login Form

In this first tutorial, you will learn how to:

  1. override the default presentation of the user login form, and create a custom template for it.
  2. pass an editable node into the user login form
  3. alter the form’s values (such as text, and instructions)
  4. make the form available to page.tpl.php — as though it was something as simple as a footer.

Moreover, I will show you how bloody simple it is. Folks who follow this tutorial should already have:

  1. knowledge of PHP
  2. the ability to lie about a lack of knowledge of PHP
  3. some familiarity of the kindergarden basics of drupal theming
  4. Be working with drupal 4.7+

Step One: Override the deafult user login form

Overriding is always done in the theme’s template.php file (if you don’t have a template.php file, you may create a it now). Obviously, before you can override anything, you must first locate what you are trying to override. The best way to do this is to search a module for the $form variable.

Building Dynamic CSS Classes With Taxonomy

Michael Angeles' post on creating different looks for different sections by switching body classes and ID's inspired me to write up a related method.

The advantages of this method are that it dynamically builds classes on the basis of vocabulary and term that allow you to have wildy different styles for every node.

This method requires two parts, one snippet in your template.php file, and a special body tag in your page.tpl.php file.

On the Joys of Raising Free-Range node.tpl.php Links

Ah, chickens -- delicious chickens. Its a timeless truth, however, that not all chickens were born equal. And clearly, the best tasting chickens are those who've been allowed to feed, graze, and socialize on the open range.

Subscribe to RSS - template.php