Twig Template Inheritance

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:04
    with Leanna Pelham
  • 0:08
    If we view the HTML source of our project so far,
  • 0:11
    we'll see just the HTML tags and printed variables
  • 0:14
    from our homepage.twig file.
  • 0:16
    So far there's no HTML layout, head, or body tags.
  • 0:20
    But since our project has been ugly long enough, let's add these.
  • 0:25
    To add the layout, there's nothing technically
  • 0:27
    wrong with including it right in homepage.twig.
  • 0:31
    This is perfectly straightforward and has nothing to do with Twig.
  • 0:38
    But let's set up the second page, /contact.
Loading ...

Twig Template Inheritance

Loading...

If we view the HTML source of our project so far, we’ll see just the HTML tags and printed variables from our homepage.twig file. So far, there’s no HTML layout, head or body tags, but since our project has been ugly long enough, in this tutorial we'll add these. Instead of just putting these in our homepage file, we're going to make this more flexible by using template inheritance, so we can reuse these pieces in other template files as we grow our site. We'll be diving into the world of Twig extends and blocks, and talking about how to avoid common mistakes.

Downloads: 
Log in or sign up to download companion files.
Additional resources: