Providing new template variables (preprocessing 1)
This screencast introduces the concept of preprocess functions – functions usually used for adding new variables to your template files. By using preprocess functions you can move complex logic out of the template files, and feed the templates with pre-made variables instead. This is useful for two reasons:
- Template files become easier to read if there is a minimal of PHP in them
- Template files may be used a lot of times on a single page load. Moving complex stuff into preprocess functions is good from a performance point of view.
Preprocess functions are places in the template.php file of your theme – the theming ninja's weapon of choice. The name of the preprocess function should be YOUR_THEME_preprocess_THEME_HOOK, which for example could translate to jean_preprocess_block. (Template suggestions aren't used in preprocess functions – only the 'base' name of the template file is relevant.)
When adding new variables to template files, make sure to end any variable name with unsafe content with '_unsafe'. That will help people to use it in a safe way.