I have a value in page.tpl.php that I would like to set in page callbacks, independently from the main content.

In page.tpl.php:

<?php print $placeholder ?>

<?php print render($page['content']); ?>

What I would like to do in the page callback:

function my_module_page_callback_foo() {
  $placeholder = 'foo';
  return 'Page Content';

Obviously, that isn't going to get me very far.

I'm hesitant to use hook_preprocess_page because then I'll have unnecessary conditional logic to determine what the menu item is (won't I?). Is there another, 'drupal way' of doing this?

I'm coming from a more 'Zend-y' world where I have access to my view and my layout variables from the controller.


There are a few possibilities, not sure which one would work best for you. There isn't a really easy way to do that.

I would say the "drupal way" of doing that would be to expose your additional content as a block. You can configure that to only show up on specific pages and place it above the main content.

There is also hook_page_alter(), but that requires the same conditonal checks as a template_preprocess.

You could also return a render array in your page callback and then display it separately in your page.tpl.php, something like render($page['content']['system_main']['part_a']) and so on. You could do that only in a template suggestion for a specific page, like page--yourpath.tpl.php.

To avoid messing with page.tpl.php too much, there is also the possibility of overriding the so called 'delivery_callback' (see menu_execute_active_handler() in your menu router item for that page. The default delivery callback is drupal_deliver_html_page().

  • Hmm. You give me multiple choices. How do I select the part of your answer that I actually found useful?
    – meriial
    May 4 '11 at 9:03
  • By the by, the part I found useful was the render array. I would like to select that as my answer.
    – meriial
    May 4 '11 at 9:04
  • You can only accept/vote the full answer, but you can state in a comment (what you already did ;)) which suggestion you actually liked/implemented.
    – Berdir
    May 4 '11 at 9:37

Since you're adding this directly into your page.tpl.php it seems like this should stay part of your theme and not deviate in origin. Not that I actually advocate adding things directly into the page template at all. Why not just create a region and place what you need into a programmatic block?

Anyhow, here is a theme only approach...

function MYTHEME_preprocess_page(&$vars, $hook) {
  $vars['placeholder'] = MYTHEME_build_placeholder();

function MYTHEME_build_placeholder() {
  $output = 'Hello!'
  return $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.