I have a custom tpl.php for my custom content type. As part of its content it also needs to show a (dynamic) value. The dynamic content is actually scraped from another website of ours.

My concern is, whether to have scraper logic inside tpl.php. My understanding is that the intention of tpl.php is to handle the view part.

What are the disadvantages of having my tpl.php to contain the logic of scraping(not just scraping. Basically anything apart from rendering)?

  1. It's bad practice from a general development point of view (look up "separation of concerns")
  2. It can break caching, or render it useless/pointless.
  3. One bit of code should do one thing, and do it in a such a way that is testable.

In my opinion, your logic for scraping should be in its own class, a class that does nothing but scrape. Then your preprocess function should make use of that class, and prepare the variables for the template.

You don't have to do any of this, of course, it's up to you what best practices you want to follow. If you're new to development, take it from me that modularising your code now will probably save you a lot of time when you come back to in 2 years and something doesn't work.

| 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.