How does theming work in drupal 6

Posted by: 
Dominique De Cooman

You ll use the drupal theming system because you want to seperate presentation from logic.
The possibilities
A simple theme function like in drupal 5
In drupal6 when adding a new theme function you need to flush the cache so drupal will rebuild its theming registry. (admin/settings/performance near the end push the clear cache button) You should install http://drupal.org/project/admin_menu it has a clear cache link too. And it is just a very handy module to administer your site.

  • First register your theme function in hook_theme() http://api.drupal.org/api/function/hook_theme/6
  • <?php
    function tandem_misc_theme() {
      return array(
        
    'sponser_link' => array(
          
    'arguments' => array('link' => NULL),
        ),
      );
    }
    ?>

  • Now you'll write your function you registered
  • <?php
    function theme_sponser_link($link) {
      return 
    check_url($link);
    }
    ?>

  • This function you ll be able to override like in drupal5 by putting this in your template.php of your theme.
  • <?php
    function phptemplate_sponsor_link($link) {
      
    $link '<a href="http://ietsanders';
    ">http://ietsanders';
    </a>  return 
    $link;
    }
    ?>

  • Wat we did now can be done differently in drupal 6. We now have a template preprocess function, these functions are used to build new variables and execute logic so you dont have to do it in the template file.

How to use a preprocess function and a template file?

  • Register your function in hook_theme() and make sure that in your array the key "template" has the value your file name.
  • <?php
    function tandem_misc_theme() {
      return array(
        
    'sponser_link' => array(
          
    'template' => 'sponser_link',
          
    'arguments' => array('link' => NULL),
        ),
      );
    }
    ?>

  • You 'll now make the file sponser_link.tpl.php and in the file you ll only use html and variables, you dont put any logic in this file. You can put the file in your module folder or in your theme folder. If they are in both the one in the theme folder will be taken. Now put this in the file:
  • <?php
    //tpl file
    print check_url($link);
    ?>

  • If you want to change the link or build another variable with it. You ll use the template_preprocess function. Put it for example in your template folder
  • <?php
    function phptemplate_preprocess_sponser_link(&$variables) {
      
    $variables['link'] = '<a href="http://somethingelse.com';
    ">http://somethingelse.com';
    </a>  
    $variables['another_link'] = '<a href="http://somethingotherthatelse.com';
    ">http://somethingotherthatelse.com';
    </a>} 
    ?>

  • Like this all logic stays separated from the presentation
  • For more read http://drupal.org/node/165706 and the hook_theme api documentation http://api.drupal.org/api/function/hook_theme/6

Add new comment