Templating a Module...?
Hey guys,
I'm writing a module that renders a block. My problem is that I can't seem to get the module to use the template file the way I want it do. I have this in the module:
function myModule_theme() {
return array(
'myModule_banner' => array(
'arguments' => array('banners' => array()),
'template' => 'myModule-banner',
),
);
}It's picking the hook up, because I can see it (and the proper template reference) in the theme registry.
Then I'm using the hook_block function to do the processing for the block. I guess I'm a little confused as to how to reference the theme function. If at the end of the hook_block function, I use:
$block["content"] = "It works!";
...then I get "It works!" rendering where the block should be. However, if I do:
$block["content"] = theme("myModule_banner", $banner); // where $banner is an array of data I want to pick up in the template file
...then it never even calls the theme file. It renders a blank block, and no matter what I var_dump or whatnot in myModule-banner.tpl.php file nothing ever shows up (I have a mix of var_dump and HTML, so SOMETHING should show if it was calling the template file at all).
Any ideas? What am I missing? The alternative is to rename the template file block-myModule.tpl.php and put it in the theme directory, and this works, but I'd really like to keep the theme file with the module files. Suggestions?
Thanks,
-Nate

Are you clearing your cache
Are you clearing your cache in admin/settings/performance?
http://w5pc.org
Default
Do you also have a default node.tpl.php ?
I believe you need one for your own template file to kick in ?
KH
www.groundlift.org
www.colindevlin.com
www.doristime.com
Yes, and yes.
I am clearing my cache...that's how I got the hook to show up in the theme registry.
I do have a default node template, but should that really matter for a module template file??
When I try to var_dump the results of the theme() function (as per above), it returns NULL...what should that function be returning when using a template file?
Thanks!
-Nate
Your hook_theme is ok
I copied your hook_theme exactly as posted into a test module and it works fine. Here's the code from myModule.module:
function myModule_theme() {
return array(
'myModule_banner' => array(
'arguments' => array('banners' => array()),
'template' => 'myModule-banner',
),
);
}
function myModule_block($op='list', $delta=0) {
if ($op == "list") {
$block = array();
$block[0]["info"] = t('My module block');
$block[0]['cache'] = BLOCK_NO_CACHE;
return $block;
}
else if ($op == 'view') {
$banners = array("I'm a banner", "I'm a banner too!");
$content = theme('myModule_banner', $banners);
$block['subject'] = 'My module block';
$block['cache'] = BLOCK_NO_CACHE;
$block['content'] = $content;
return $block;
}
}
And here's myModule-banner.tpl.php:
<p><ul><?php foreach($variables['banners'] as $text) print "<li>$text</li>" ?>
</ul></p>
It's rendering the template just fine.
http://w5pc.org
Thanks! One More Question...
Ok, I see. I wasn't reading the variable correctly. I was troubled that theme() wasn't giving me any output, and then I was trying to read the variable from the block's content instead of just assuming that $banners was passed. Thanks for clearing that up.
One more question: It looks like some extra HTML is being rendered around my template file. For instance, when I look at the rendered code for my block, I see the following:
<div id="block-myModule-0" class="block block-myModule"><h2 class="title"/>
<div class="content">
<!-- Contents of Template -->
</div>
</div>
Is there any way that I can override those H2 and DIV.content so that they don't show up? I know I can hide the H2 via CSS, but I'd love if it wasn't in the mark-up at all. How do I override that from the module code?
Thanks!
-Nate
Create a file called
Create a file called block-myModule-0.tpl.php in your theme directory. Copy the contents of block.tpl.php into it, then delete the parts you don't want.
You can't remove this formatting from within your module though, as it is rendered outside the theme, and the theme is inserted in to it. The only possible way you could do it is to create a module that overrides this block rendering, then add that module as a requirement to your own module. But, you shouldn't do this if you are planning on releasing your module at all, as it's overriding standard Drupal rendering, which many themes will depend on.
If it's a padding/margin problem, you can just set the margin and padding of that bock to none, and render the h2 tag as hidden, using CSS inside your module.
edit: you can also go into admin/build/blocks, click 'settings' for the block in question, and set as the title. This will remove the h2 tag.
It all looks fine to me, the
It all looks fine to me, the only think I can think may help is changing this:
'arguments' => array('banners' => array()),to this:
'arguments' => array('banners' => NULL),then visit admin/build/theme which will rebuild your theme registry.