Templates or Preprocess Functions: Where Does My Logic Belong?

guschilds

We spent years telling our peers that their logic didn't belong in our templates. Preprocess functions are for logic, we said. But then Twig came along and we're jumping at the opportunity to fill our templates with logic. Is that ok? Preprocess functions haven't gone anywhere, so where does my logic belong?

Where the logic should live depends on what it is doing. Fewer preprocess functions means less confusion and less PHP for front-end developers, but they still serve a purpose. That said, the rest of our logic (a majority, perhaps) can move into our templates.

This session will cover:

  • what preprocess functions are and how they work
  • what type of logic still belongs in preprocess functions (with examples)
  • what type of logic should be moved to Twig templates (with examples)

Previous experience with the theme system will make this session easier to follow.

Session Track

Front End

Experience Level

Intermediate

Drupal Version