0
votes

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.

1
What does the PHP file look like where you call Timber::render()?Gchtr
Well it did look like Timber::render( 'base.twig', $context ); and I found out I have to render the test.twig for it to work. However I'm still not clear then how to manipulate multiple block elements in the same twig file. I edited my original post.RandymWebDev
What do you mean about doing multiple block nesting? Have you taken a look at this? twig.symfony.com/doc/2.x/tags/extends.html Does it answer your questions?Matias Kinnunen
Thank you for that link, I hadn't seen it yet. It is helpful but I'm not completely clear yet. It states that "So you can only have one extends tag called per rendering". Ok but then in the first code example there is both a block head and a block footer in the same twig file. So I would assume that I could have 2 separate twig files that extend for each of those blocks. But I guess that is wrong, so I could only have one child twig file that will extend to both of those block elements? Is that the way things are done?RandymWebDev

1 Answers

2
votes

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.