Reputation: 18
I have been following the documentation, tutorials, and some youtube videos, I can never get block nesting and "Extends" to work. Aside from that Timber seems to be working properly.
In the views folder I have these 2 files:
base.twig
test.twig
in base.twig
{% block content %}
........
{% endblock %}
in test.twig
{% extends "base.twig" %}
{% block content %}
This is a test
{% endblock %}
From what I have been following, the code is correct, and I should get an output of "This is a test", right? But instead I still get ".....".
----EDIT----
Ok I figured out some the problem, in my php file I was rendering base.twig. Which apparently didn't do anything to associate test.twig. So when I render test.twig I get the behavior I'm looking for.
However that doesn't explain how to do multiple block nesting... I can nest several files deep inside the same block.. But what if you have two separate blocks in the same twig file? What is the method for dealing with multiple blocks? I can't find documentation on that or any case examples.
Upvotes: 0
Views: 698
Reputation: 175
I think you may be confused about how blocks and extend work...
If you really want to see good examples of this in action, check out the Timber Starter theme and look through the various .twig files they provide out of the box. If you are building a site with Timber I highly recommend you use this theme to get started.
In regards to your latest comment (I don't have enough rep to comment otherwise I would) the example has a block head
& block footer
inside of the base.twig
. So when you extend this base.twig
file in a new twig file, say: home.twig
for example:
{% block head %}
This is your home.twig head content, which will replace base.twig content
{% endblock %}
{% block footer %}
This is your home.twig footer content, which will replace base.twig content
{% endblock %}
So in your base.twig, these blocks are dynamic placeholders. When you extend base.twig and add new content inside of these blocks, they replace the content inside of base.twig.
As far as I know you can only extend a single file at a time. You can customize and create as many layouts as you like and then extend those. So you might have a base layout that you would extend for the majority of your pages. But maybe you need another layout with a sidebar, which you can extend on pages that need a sidebar. You could create another layout for an eCommerce section of your site etc. The whole point is to keep you from having to duplicate a bunch of code across pages, by following the DRY principle.
I hope that makes sense, if not, let me know and I will be more than happy to help you.
Upvotes: 2