Create custom variables for a template file inside a menu callback function here is a good one that has always stumped me.

I would love to see a solution in both D6 and D7.

I create custom modules and usually work with the menu api to create menu type -> MENU_CALLBACK.

Here is an example:

function example_menu() {
  $items['example/page1'] = array(
    'page callback'     =&gt; 'example_page1_callback',
    'access callback'   =&gt; TRUE,
    'type'              =&gt; MENU_CALLBACK,
  );
  return $items;
}


I would like to see the exact code solution to provide three different variables to be printed in a custom theme template based on the following code.  To add to that challenge, I would like the code to be written in the menu callback function only.  I know there are many possible ways to do it but could never figure out how to do it in a menu callback function which would be the most useful way.

//****************************************
// This code block goes in a custom module called example_menu_variables.module

// hook_menu example
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     =&gt; 'example_page1_callback',
    'access callback'   =&gt; TRUE,
    'type'              =&gt; MENU_CALLBACK,
  );
  return $items;
}

// hook_meny callback function
function example_page1_callback() {
  // please provide code within this function to turn these next three
  // variables into separate output variables for the template.
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');
  return;
}
//****************************************



//****************************************
// This block of code represents the part of the custom theme template related to the menu location from the hook_menu call
// D6 template name would be page-example-page1.tpl.php
// D7 template name would be page--example--page1.tpl.php

&lt;div&gt;&lt;?php print  $variable_to_print1?&gt;&lt;/div&gt;
&lt;div&gt;&lt;?php print  $variable_to_print2?&gt;&lt;/div&gt;
&lt;div&gt;&lt;?php print  $variable_to_print3[0] . ' ' . $variable_to_print3[1] . ' ' . $variable_to_print3[2]?&gt;&lt;/div&gt;

/*
output should look like:
Foo
Bar
bing bang bong
*/

//****************************************


Hope this is the kind of problem that someone wouldn't mind solving.  Thanks for your time.  :)

Ray James
I don't believe you can do this without using hook_theme, but this is exactly what it's for and it's quite simple. (This code is for Drupal 6.)

/**
 * Implements hook_menu().
 */
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     =&gt; 'example_page1_callback',
    'access callback'   =&gt; TRUE,
    'type'              =&gt; MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback function
 */
function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  return theme('example_contents', $variable_to_print1, $variable_to_print2);
}

/**
 * Implements hook_theme().
 */
function example_theme() {
  return array(
    'example_contents' =&gt; array(
      'arguments' =&gt; array('variable_to_print1' =&gt; null, 'variable_to_print2' =&gt; null),
      'template' =&gt; 'example-contents',
    ),
  );
}


$variable_to_print1 and $variable_to_print2 are now available in example-contents.tpl.php.