Templatephp Gotchas

When using a template.php file in PHPTemplate themes, there are a couple of things you might want to watch out for.

First, you can only override a theme function once. While this seems simple, there is one circumstance in which this may be surprising.

While you can declare a template.php file in a sub-theme even if there is a template.php in the parent, you cannot override functions declared in the parent. If the parent's template.php declares phptemplate_breadcrumb(), then the sub-theme's template.php cannot declare phptemplate_breadcrumb(). Doing so will result in a PHP errors for redeclaring a function.

Second, we have seen that theme hooks (theme_<hookname>()) can be overridden by PHPTemplate templates and also by functions in the template.php file. But what happens if you declare both?

The phptemplate_<hookname>() function overrides the theme function that would otherwise load the template. Therefore, if phptemplate_block() is defined in template.php and there is a template named block.tpl.php, the phptemplate_ block() function will be called to render all blocks, and the block.tpl.php file will never be used.

If you need to manipulate some content before it is passed to a template, don't do this by defining phptemplate_<hookname>(). Instead, use one of the preprocessor functions, such as phptemplate_preprocess_block(), phptemplate_preprocess_ node(), or phptemplate_preprocess_page() .

At this point, we've created a new theme. We've added custom CSS, images, PHPTemplate files, and even some custom theme hooks... what else does it take to complete a theme?

One more thing.

0 0

Post a comment