Aha! Understanding and Using Render Arrays in Drupal 8
theme() has been a popular function used by Drupal developers since Drupal 4.7. Drupal 7 core calls theme() over 200 times alone. But theme() no longer exists in Drupal 8 and there is no object-oriented equivalent. If we can’t call theme(), how do we render our data with the Twig templates that we’re all so excited about?
Render arrays. Every use of a Drupal 8 template starts with a render array and many variables within a template are render arrays themselves. While they did exist before Drupal 8, render arrays weren’t used as often as they should have been. They’re everywhere now, and for good reason. They provide many advantages over their deprecated counterpart.
Unfortunately render arrays are a bit confusing and intimidating until that wonderful “aha moment” hits. This session aims to provide that moment. It will cover:
- what render arrays are and how to use them
- how render arrays become rendered markup
- why render arrays provide advantages over theme()
- best practices for using render arrays in a sustainable way
- other goodies, such as attaching assets and caching
Because render arrays bridge the gap between the back and front ends, it’s important for developers on both sides to understand them. Previous experience with theme(), hook_theme(), preprocess functions, and/or templates in Drupal 7 will make this session easier to follow and understand, but isn't required.