1

Since templates are slower than theme hooks, I'm wondering if I might be able to use them for my application. Here's what I'm trying to do:

Right now I have about 10 content types on my site and I've created a separate template file for each because I want each to have a <div> with a specific 'id' so I can target those pages with CSS. I also give each a separate template file so I can determine on a case by if they should have a secondary sidebar or not.

Those are the only reasons I'm using all these template files.

Two Part Question:

Can what I'm trying to do be reasonably accomplished with theme hooks, and if so, how?

2
  • Since templates are slower than theme hooks where did you get this from?
    – No Sssweat
    Feb 12, 2017 at 6:45
  • You don't have to create 10 different template files for this, you can access the content type name in twig with node.bundle|clean_class and build a specific id.
    – 4uk4
    Feb 12, 2017 at 11:22

1 Answer 1

3

The module that defines a template defines if it is a template or a theme functions, that's not something you have any influence over.

And yes, theme functions are faster, but they're also deprecated and will be removed completely in Drupal 9 (Because twig is far easier to write than PHP). With render caching and so on, the performance difference is no longer as relevant as it used to be.

The performance difference is between having a template or not. There's anyway a template, just not a custom one, and the only performance difference then is the additioal complexity that your template adds and what you are doing sounds trivial.

There might be reason to do what you're doing differently, you could use preprocess, but you'd need to at least share your templates/code (you should always do this when asking questiosn) so you can get a specific code example. But not because of performance, a more valid reason would be code/template management, it will be more work to make further changes if you have 10 different templates or just one.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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