0

What is performance cost of executing queries like EntityFieldQuery directly in tpl.php files ?

I have some situations, where i have put db queries and more complex php logic in various tpl.php files.

It works, but my concern is performance. What are best practice ? Should i move those queries somewhere else and what are advantages ?

Thanks!

2

To be honest you should never, ever run an SQL query inside a template file. This sort of thing should be handled either at the preprocess level, or even better, at an earlier stage in the page build (usually in a custom module file). There are good reasons for this:

  1. Putting business/application logic inside a template file is never a good idea. Literally reams of volumes have been written on the subject; over the past few years MVC has been the 'it' pattern so I'd recommend reading up on that to understand the reasoning behind this kind of separation.
  2. If you execute queries in your template files they will run even for pages for which the rest of the page content has been cached, which is a definite performance no-no.

I imagine there are other good reasons not to do it as well, suffice to say these are two quite strong arguments for moving the logic out of your template files.

| improve this answer | |

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.