How do I use hook_theme() in a module? I need help in using hook_theme in my custom module. Any good examples is much appreciated. I have created a CCK input form and I want to theme the input form using hook_theme. The content type is created using hook_install().

I have used hook_form_FORM_ID_alter() to alter the theme of the input form. Following is the code for that. 

 function footer_menu_form_footer_menu_node_form_alter(&amp;$form,&amp;$form_state,$form_id) {
   theme('footer_menu_node_form', array('form' =&gt; $form));
 }

 function footer_menu_theme($existing, $type, $theme, $path) {
   return array('footer_menu_node_form' =&gt; array(
     'arguments' =&gt; array('form' =&gt; NULL),
     'template' =&gt; 'footer-menu-node-form',
     'render element'=&gt; 'form',
   ));
 }


I have also created a footer-menu-node-form.tpl.php file inside the module, and the following is the code in the template file.

 &lt;div id="title"&gt; &lt;?php print drupal_render($form['title']); ?&gt;
 &lt;div id="tree"&gt;
 &lt;h2&gt;Menu Tree&lt;/h2&gt;
  &lt;/div&gt;
 &lt;?php print drupal_render($form['title']); ?&gt;
 // this one is for rendering all the remaining items, like hiddens:
&lt;?php print drupal_render($form); ?&gt;


When I load the footer menu content type. The content within the &lt;h2&gt; tag is alone printed and then I get the following error:


  Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40961 bytes)


Can anyone tell me what I doing wrong, or could anyone suggest me an efficient way to use hook_theme() in modules?
The proper way to assign a theme function to a form using hook_form_alter() is like this:

function footer_menu_form_footer_menu_node_form_alter(&amp;$form,&amp;$form_state,$form_id){
  $form['#theme'] = 'pz_footer_menu_node_form';
}


I can't tell if you're using Drupal 6 or 7; you're using the arguments property in your theme declaration which is only valid in Drupal 6, and also the render element property which is only valid in Drupal 7. 

Judging by the out of memory error I'm guessing Drupal 7. As you call drupal_render() on the the $form object in your template file, it runs that variable back through the same theme function, and you end up with an infinite loop.

Try changing the last line in your template file to this:

&lt;?php print drupal_render_children($form); ?&gt;


That should stop the error from happening.