[themes] how toi theme a box and how to theme a file ?

Larry Garfield larry at garfieldtech.com
Thu Oct 29 03:14:06 UTC 2009


On Wednesday 28 October 2009 5:35:35 am Michel Morelli wrote:
> Hi all. I have a template file called "foo.tpl.php", and I have $vars.
>
> How can i theme "foo.tpl.php" with $vars ?
>
> I have thinked that I need to use "theme" function(), but how ?
>
> And for a box ?
>
> M.

You're actually putting the cart before the horse, it looks like.

Somewhere in code, you'll need to format some data.  OK, you want to use a 
template.  So you first add a hook_theme[1] entry to define your theme key 
(foo), and you specify that it uses a template file (foo.tpl.php by default).  
Then you call:

$output = theme('foo', $somevar, $anothervar, $somethingelse);

in your module code where you want to turn $somevar, $anothervar, and 
$somethingelse into rendered HTML.

With the theme hook setup correctly, that will result in foo.tpl.php getting 
called with $somevar, $anothervar, and $somethingelse, along with various 
other globally added variables.  You can also use a template_preprocess_foo() 
function of your own to filter and mutate $somevar, $anothervar, and 
$somethingelse as needed or add more variables at that point.  see the theming 
guide[2] for more information.

You should start thinking from the point in code where you need to have 
something rendered, not from the template and then finding a place to put it.  
It will conceptually make more sense that way when you implement it.

[1] http://api.drupal.org/api/function/hook_theme/6
[2] http://drupal.org/theme-guide/6

-- 
Larry Garfield
larry at garfieldtech.com


More information about the themes mailing list