开发者

Howto output HTML from drupal module the nice way

开发者 https://www.devze.com 2023-03-03 01:44 出处:网络
this may be basic, but i just started using Drupal (6 btw). I\'m building a module, and got a callback that is supposed to return some html. I could just do something like this:

this may be basic, but i just started using Drupal (6 btw).

I'm building a module, and got a callback that is supposed to return some html. I could just do something like this:

function myModule_myFunction(){
    $r = '';
    $r .= '<h1>'.$variable.'</h1>';
    return $r;
} 

But I'd rather seperate logic and presentation, and put this in a seperate file, or something. HTML in strings is ugly!

What's the 'drupal' way to do this? It doesn't have to use the theme 开发者_运维技巧system as far as I'm concerned.


The theme system is the drupal way to do that.

First check if there isn't already a theme function that you can use, there are many that allow to output data as table, lists, images and much more.

If there is not, you need to...

  1. Declare a new theme function with hook_theme(). Prefix it with your module, something like yourmodule_something.

  2. Create a theme function (or a template), a theme function must be prefixed theme_, so something like function theme_yourmodule_something().

  3. Make sure to clear all caches so that Drupal knows about your theme function.

  4. Call the theme function with theme(): theme('yourmodule_something', $argument1, $argument2):

More details can be found on the linked documentation pages.


hook_theme is first used to define templates and functions for themed output.

Then, use those templates or theme functions to output any markup, e.g, to call theme_my_content or my-content.tpl.php with a couple of parameters, you can call:

theme('my_content', $param1, $param2);

0

精彩评论

暂无评论...
验证码 换一张
取 消