How does theming work in drupal 6
On:
Sunday, November 30, 2008 - 14:47
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
- Now you'll write your function you registered
- This function you ll be able to override like in drupal5 by putting this in your template.php of your theme.
- 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.
<?php
function tandem_misc_theme() {
return array(
'sponser_link' => array(
'arguments' => array('link' => NULL),
),
);
}
?>
<?php
function theme_sponser_link($link) {
return check_url($link);
}
?>
<?php
function phptemplate_sponsor_link($link) {
$link = '<a href="http://ietsanders';
">http://ietsanders';
</a> return $link;
}
?>
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.
- 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:
- 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
- Like this all logic stays separated from the presentation
<?php
function tandem_misc_theme() {
return array(
'sponser_link' => array(
'template' => 'sponser_link',
'arguments' => array('link' => NULL),
),
);
}
?>
<?php
//tpl file
print check_url($link);
?>
<?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>}
?>
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