By Dave Cohen on
I noticed the Drupal 6 theme API enphasizes preprocess hooks as a way to customize behavior. It used to be possble to change the output after the template produced it. Now, I can't find a way to do this sort of "postprocessing". Help!
Here's an example of a theme_page override from Drupal 5. How can I do this in Drupal 6?
// Drupal 5 code takes the output produced by page.tpl.php, then munges it...
function fb_fbml_page($content, $show_blocks = TRUE) {
$output = phptemplate_page($content, $show_blocks);
if (function_exists('fb_canvas_process')) {
$output = fb_canvas_process($output);
}
return $output;
}
Comments
emphasis on the n
(and yes I know how to spell emphasizes. Just noticed my typo but I can't go back and edit it)
Thanks to Earl
Thanks to Earl Miles for pointing me in the right direction. This code seems to be working. (But stands little chance of working in subsequent versions of Drupal).
Subscribe. >stands little
Subscribe.
>stands little chance of working in subsequent versions of Drupal
But something like it should surely? Looks like this nifty bit of code is just emulating the behaviour of theme('page', ...) and adding your postprocessing at the end.
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
An alternate.
This will work too. It might take a tiny performance hit since the render function is intercepted for all templated hooks but it should be very minor.
A big plus on this is that you can sneak in any number of "post process" functions for any hook without recreating the flow of the theme() function.
⎋ joon park
–www.dvessel.com–
only problem with this
I don't know that this was the case in D6 but now in D7, you can't expect this to run always. render_template is only ever called when a file template is rendered... not a function template. Since most themes use functions these days as opposed to actual files, this causes the issue of missing the postprocess phase.
the "drupal" way
I've spent about 8 hours researching the same question. Then I smacked my forehead. The template file should also be considered the "postprocess" function. My example is related to theming the user registration form. First you create a theme hook for the form:
...a "PREprocess" function is optional. Use this if you want to change or add vars to use in the tpl:
...I then created user-register.tpl.php and did POSTprocessing on the $vars['form'], which is now just $form:
It's actually quite elegant. Good luck, drupalers!
I object slightly to the
I object slightly to the elegant comment as it is frowned upon to put logic/processing in a template file. I think dvessel's method above is much more elegant and "Drupal-like" as it allows any module or theme to add postprocess functions to the render array.
--
Caleb Thorne
Developer | Monarch Digital
https://www.calebthorne.com