0

I am trying to theme a data table with a tpl.php file. I am placing the theme-table.tpl.php file in the project. How do I call it to theme from within the code? The file contains html and php prints.

Thanks

  • 1
    Have you read the Theming Guide? – Clive Apr 8 '13 at 14:51
  • Apparently not. I used go through the whole guide just to find this one? Thanks for the link :-) – Potney Switters Apr 8 '13 at 14:53
  • 1
    Nah, no need to read it all but it's a good resource if you're new to theming. The hook_theme() docs are pretty helpful too if you haven't come across them before – Clive Apr 8 '13 at 14:58
1

In your mymodule.module, you should first declare a theme:

/**
 * Implementation of hook_theme.
 */
function my_module_theme() {
  return array(
    'my_module_page' => array(
        'variables' => array('mydata'=>"Default value"),
        'template' => 'my_module_page',
     ),
  );
}

The value of template is the filename of your tpl.php file.

You can use the variables array to pass data to the template, just like I will be doing here with mydata.

Then, in any function of your module, you can create a renderable array, render it using drupal_render and output it, or print it, depending on your needs.

$renderable = array(
  '#theme' => 'my_module_page',
  '#mydata' => "Hello World!",
);

return drupal_render($renderable);

So, your template (my_module_page.tpl.php) should look like:

Here is my template data : <?php echo $mydata ?>
| improve this answer | |

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.