04.
Using a Layout: Template Inheritance
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
7 Comments
hi. When paste
and go to the http://localhost/twig/index.php/contact - we have an error.
need change
<h1>{{ pageTitle }}</h1>to<h1>{{ pageData.title }}</h1>Hey Denny,
Thanks a lot! Fixed in https://github.com/knpunive... .
Cheers!
something Ive always been slightly confused about.
Why would you use a base and a layout? I dont see the benefit.
Hey!
If you *don't* extend a base layout but want a page with full HTML markup (e.g. <head>,<body>, etc) - then you can certainly just put all of that markup right inside your one template (e.g. homepage.twig). But as soon as you want to share that base layout between two templates (like contact.twig), then using a base layout allows you to do that without duplication.
However, this all assumes that you're using Twig in a system where your job in Twig is to generate the *full* page. That's not always the case. For example, in Drupal, when you use Twig for theming, you're just building little "pieces" of the page - the full layout is constructed elsewhere. In this case, it does *not* make sense to extend a base layout like this. However, even in that case, extends has some really cool uses! For example, imagine you're theming two different node types that are *almost* equivalent, but not exactly the same. Instead of duplicating all of your markup between the 2 templates, you could create a base template with all of the shared parts, surround the part that needs to change in a block, then extend that base template from both children templates and override the block. In this case, the "base template" is not a "base layout" like in this tutorial (i.e. it does not have the base <body> & <head> tags), but it serves the same purpose: to allow multiple templates to share some structure without duplication.
Phew! Does that help? Interesting question :)
many thanks for that. I'm not sure I worded my question correctly. I fully see why you would extend base.twig but...
I see everywhere, a base.twig file, then a layout.twig, which extends the base. Then all other templates extend the layout. this is what I dont really understand, it feels like making things more complicated for no reason.
Sorry to make you write all that!
Ah, it's cool - it will hopefully be a good explanation to *someone* who is wondering all of that :).
In short: there is no point to the extra "middle" level "layout.twig" file. Early in Symfony, we tended to over-engineer things, and it was really common to (A) have multiple bundles and (B) to give each bundle its own "layout.html.twig" file that all the templates in that bundle would extend. Why? Because if you add that "middle" layer, it allows you to have different "sections" if your site that have a *slightly* different layout. For example, imagine your site has a top nav. But in one section of your site, the top nav is gone (or drastically different). In this case, you create a "layout.twig" that extends "base.twig" and overrides the top nav to remove/change it. Now, you can have 5 different templates from this section extend "layout.twig" to get that new "section-specific" layout.
So, the extra "middle layer" has a good purpose. But it's not needed in most cases and you tend to see people add it when it's not needed at all. I hate that extra layer :).
Cheers!
ah cool many thanks. I hate it too, being a minimalist-purist whenever possible. It just seems to create more work in debugging. I just wanted to check as I'm helping a few Padawan learners get started and didn't want to tell them the wrong stuff.
thanks again and also thanks for the great work.
Matt
"Houston: no signs of life"
Start the conversation!