1

I wanted to add some custom functionality in my Drupal website, so I read some tutorials (first, second ) and decided to create custom template files for specific forms & exposed filters I wanted to change. And it worked perfectly!

But when I had to pass the changes to the original site I had to change the code a bit, so I tried with a custom module this time (not using template files) and it also worked like a charm!

I'm confused because both templating and custom modules can be used for providing the same functionality. But which is better - in terms of stability, performance, code reuse etc. ?

1

You should separate your templating into two logical groups:

Theme-specific Only markup and styling that is relevant to your site theme should go here. You should ask yourself the question - if I was to change my theme - would it affect any functionality of the site? For instance, say you needed to add a block of markup that inserted a third-party analytics tracking. If you put this in html.tpl.php, it would be removed if you switched theme, breaking site functionality.

Functionality-specific If you are augmenting your site with new functionality that requires markup or styling (quite rare) independent of theme, then it should be coupled with the module. The above example of inserting a snippet of JS for a third-party analytics provider is a good one, for instance. Again, ask yourself, what happens if you were to switch the theme. If any site functionality were to break because of a change in theme then it shouldn't be in the theme, but part of the module.

For the most part, your markup and styling will be related to your theme. But say you developed a forum module - you would need some base markup to deliver the functionality; base markup and styling which could then be enhanced by a specific theme, and not break the basics of the forum layout if you decided to implement or switch another theme.

1

That depends on what you would like to accomplish. When it is just pure templating (markup, styling, theming) you can do this with template files just fine.

But when you want to include custom logic (programming), it is better to create a custom module where you put the hook() implementations and/or preprocess functions etc...and then define a temaplate file there.

Does this offer some clarification for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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