Wednesday, March 2, 2011

.tpl file output using theme function

So, you have prepared the output in one of your module function with all complicated business logic client specified in last meeting.  Your "return ($output);" throws everything on screen. You became glad to see your code worked finally.
And if Table layout is required you pushed all html in $output variable and theming of your own module page become hectic. Better to go with tpl output.

function hook_menu() {
  $items['url'] = array(
    'title' => 'Title',
    'page callback' => 'menu_callback',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
  return $items;

function menu_callback() {
  $output_var1=your logic;
  $output_var2=your 2nd logic;
  $result = theme('theme_key', $output_var1, $output_var2);

function hook_theme() {
  return array(
    'theme_key' => array(
      'arguments' => array('output_var1' => NULL, 'output_var2' => NULL),
      'template' => 'tpl_file_name',
Now create your tpl file, and use $output_var1, $output_var2 for output there. and give that tpl to your designer. S/He will love you :-).
For detail understanding look into user module.

No comments: