Drupal's theme system can be a bit intimidating the first time you step into it. I’ve found this little snippet trail to be really helpful for me to get a jump start on the process.
Step 1: Hook Theme
Use hook theme to register your function and the template that is associated with it.
- /**
- * Implements hook_theme().
- */
- function dm_message_theme() {
- return array(
- 'message_template' => array(
- 'variables' => array(
- 'type' => NULL,
- 'vars' => array(),
- ),
- 'template' => 'messages',
- 'path' => drupal_get_path('module', 'dm_message'),
- ),
- );
- }
Step 2: Call the Theme Function
You can call the theme function wherever you would like. Just use this syntax:
- theme('message_template', array('type' => $message->type, 'vars' => $vars));
Step 3: Pre Process Function
Chances are you will want to format your variables before going into the template file. The system automatically searches for pre-process functions with your template files name:
- function dm_message_preprocess_message_template(&$vars) {
- }
Step 4: Write the Template File
Template files should be structured. Processing should be done before calling the theme function and more complicated formatting should be done in the pre-process function. Leaving a template that looks a bit like this:
- <div class="message">
- <div class="message-region message-region-left">
- <?php if (isset($image)) : ?>
- <div class="message-item-left message-image"><?php print $image; ?></div>
- <?php endif; ?>
- <?php if (isset($image_subtext)) : ?>
- <div class="message-item-left message-image-subtext"><?php print $image_subtext;?></div>
- <?php endif; ?>
- <?php if (isset($image_subtext2)) : ?>
- <div class="message-item-left message-image-subtext2"><?php print $image_subtext2;?></div>
- <?php endif; ?>
- </div>