6

I want to add some variables to page.tpl.php via template.php so i guess i need to use my_theme_preprocess_page. But my variables does not seem to be passing through.

I am using something like this :) In template.php

function my_theme_preprocess_page(&$vars) { 
$vars['myvar'] = "value";
}

and in page.tpl.php

print $myvar;

and $myvar fails; also tried defininig it with $vars['page']['myvar'] = "value"; and reading it back with print ($page['myvar']); but with no luck, variable (or index) stays undefined.

I cleared cache.

Any clues why this does not work ? Thanks!

5
  • does it run into function my_theme_preprocess_page(&$vars)? If you write an exit into that the page dies? – hosszu.kalman Jan 8 '13 at 20:10
  • Hey, can you please explain more in detail, what does seem suspicious to you ? Thanks! – NenadP Jan 8 '13 at 20:23
  • Code looks ok, presumably the preprocess_page hook isn't being called, or is for the wrong theme. – David Thomas Jan 8 '13 at 22:07
  • 1
    Add watchdog() calls into your my_theme_preprocess_page() function to verify that it is actually being called. I suspect that @david-thomas is on the right track. – Gold Jan 8 '13 at 22:25
  • Adding watchdog helped me to find that the function template_preprocess_page is not even being called! (while template_preprocess_node is working correctly). Also i found out that template_preprocess_html does not work too! Any clues why those function fail to execute in template.php ? (of course both page.tpl.php and html.tpl.php are present in template folder...) – NenadP Jan 10 '13 at 13:19
2

Drupal doesn't have code that checks if the theme is adding a new index in the array passed as reference to hook_preprocess_page(); every index added to that array from a preprocess/process function is then passed as PHP variable to the corresponding template file.

If what you are adding is then not passed to the template file, then the possible explanations are:

  • You are prefixing the function name with something that is not the short name of the theme.
  • You wrote the preprocess function for the theme that is not currently active. If the theme currently used is the Bartik theme, the preprocess function for a different theme would not be called. Keep in mind that the used theme could be different from the one you are expecting; this is what happens with administration pages, for which the used theme is the one set as administration theme in admin/appearance.

    screenshot

    There are also modules that can set a different theme for a page implementing hook_custom_theme().

A possible mistake could be implementing a preprocess function for a theme when a sub-theme is actually used, or (vice versa) implementing a function for a theme when the parent theme is actually used.

References

1
  • Thanks for very descriptive feedback! I am using my custom theme for viewing site, and "seven" for administrative theme. I have put watchdog() and found that the function template_preprocess_page is not even being called. (while template_preprocess_node is working correctly). Also i found out that template_preprocess_html does not work too. Any clues why those function fail to execute in template.php ? (of course both page.tpl.php and html.tpl.php are present in template folder...) – NenadP Jan 10 '13 at 13:31
2

Add the following to template.php for your theme. Replace themename with the effective short name of the theme.

function themename_preprocess_page(&$variables) {
  $variables['contactTitle'] = t('You can leave a message using the contact form');  
}

Clear the cache, and add this anywhere in the page.tpl.php file used by your theme.

print $contactTitle; 
0

Thank you all for your effort to resolve this issue. The error was of very strange origin. Drupal cache was not cleared when doing drush cc - all!

Cache was fully cleared when I cleared it from Drupal performnce clear cache button. This is strange new issue that is part of something else.

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.