Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Semi-new to Drupal. I'm trying to find a way to render HTML inside my custom module.

Right now I'm using this method

$page = 'hello world';
return $page;

Which can get pretty dirty once you had some logic into it.

Is there a better way to do this? Maybe render it to some kind of template file and pass an array of vars?

Any help is appreciated :)

share|improve this question
5  
"Maybe render it to some kind of template file and pass an array of vars?"...yep, you've just described Drupal's theming system :) See hook_theme() – Clive Apr 30 at 13:52
...or if you just need to wrap your code in some DIVs, just create the render array and Drupal will take care of the hard work. – Ayesh K May 2 at 1:08

1 Answer

Clive is right that using the drupal theme system is your best bet if you're going to be doing significant variable manipulation but if you just want to load a big blob of HTML into your module you can do it a couple of ways in PHP.

For short blobs, try the EOF method

$str = <<<EOF
  <p>Hello</p>
  <p>$world</p>
  ... etc.      
EOF;

For longer blobs, use file_get_contents()

$myhtml = file_get_contents(path_to_module().'/my_html.tpl');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.