How to set variables in a page callback and use it in the page template

Usually we set variables in a page callback to be used in a template, that template will be render in the main content of the page, but what if we want to use that variable into the drupal page template, that’s not so easy.

Here is a simple snippet to do it in Drupal 7.

In .module file
function example_page1_callback() {
$render_array['variable_to_print1'] = 'Foo';
$render_array['variable_to_print2'] = 'Bar';
// set other render array items for the page
return $render_array;

In page.tpl.php file

1st variable = <?php print render($page['content']['system_main']['variable_to_print1']); ?></div>

2nd variable = <?php print render($page['content']['system_main']['variable_to_print2']); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.