Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7 - How to load a template file from a module?

I am trying to build my own module in Drupal 7.

So I have created a simple module called 'moon'

function moon_menu() {
  $items = array();
      $items['moon'] = array(
      'title' => '',
      'description' => t('Detalle de un Programa'),
      'page callback' => 'moon_page',
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
  );

  return $items;
}

function moon_page(){


$id = 3;

$content = 'aa';

}

in moon_page() function, I like to load a custom template 'moon.tpl.php' from my theme file.

is this possible?

like image 409
Moon Avatar asked Mar 14 '11 22:03

Moon


2 Answers

/*
 * Implementation of hook_theme().
 */
function moon_theme($existing, $type, $theme, $path){
  return array(
    'moon' => array(
      'variables' => array('content' => NULL),
      'file' => 'moon', // place you file in 'theme' folder of you module folder
      'path' => drupal_get_path('module', 'moon') .'/theme'
    )
  );
}

function moon_page(){

  // some code to generate $content variable

  return theme('moon', $content); // use $content variable in moon.tpl.php template
}
like image 110
dobeerman Avatar answered Sep 28 '22 09:09

dobeerman


For your own stuff (not overriding a template from another module)?

Sure, you only need to:

  • Register your template with hook_theme()

  • Call theme('moon', $args)

$args is an array that contains the arguments to the template as specified by your hook_theme() implementation.

like image 28
Berdir Avatar answered Sep 28 '22 09:09

Berdir