1

Possible Duplicate:
When should I add a new tpl.php file, and when should I implement a theme function?

To theme a form: There are two ways.

  1. using function theme_themename_form($form)

  2. Assigning to a variable in preprocess and printing it in the template files.

My question is:

  1. Why two ways?

  2. Which way is best for which case ?

    That is for which type of cases we should go for function theme and for which cases we should go for template files.

  3. What is all time best way?

0

2 Answers 2

2

I always think of it like this:

If you need to write a fair bit of HTML for your theme then you want a template file. This way you can maintain as much separation HTML and PHP as you'd like.

If you theme is just accepting data and putting that data through (say) another theme function, then you're not going to be writing any/much HTML. In this case a theme_* function is more appropriate in my opinion.

I don't think there really is an "all time best way", both methods are appropriate for different situations.

1

Both theme functions and template files are useful under different contexts. Tpl files are used if there isn't much preprocessing logic involved. These are the views equivalent in an MVC architecture. So, the maximum allowed logic here is "if-else" constructs, and even that is debatable.

Example use cases for tpl: If you want to change the basic structure of an entity, like print the taxonomy tags as CSV links instead of the default div structure.

Theme functions OTOH, allow for more granularity like augmenting class and other attributes. Preprocessing logic mixes well here as it is till in the "Controller" part of MVC and is not rendered yet. Example use case for theme functions: add a class based on the taxonomy term value

In the real world, the demarcation between views and controllers is really thin. Drupal gives the provision(flexibility rather) of having themeing done at both levels for different contexts.

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