3

I have a _layout.html template which has both an include partial {% include '_partials/_header.html' %} and a block section (e.g. Content) for other templates to extend it.

Can I put another block section (e.g. Breadcrumb) inside the include file and use that in a template which extends _layout.html? So far it doesn't seem to work. If I move the Breadcrumb block from the include template into _layout.html it works fine, and when I move it back the content in the template extension does not show.

  • Could you provide an example of your code as a twig fiddle at twigfiddle.com – Fyrebase Aug 3 '16 at 3:33
4

I believe it is possible @jack. Take a look at this working demo - http://twigfiddle.com/9rwjeo


Update:

On closer inspection the original example did not work. You can not have a block inside an include how you are describing.

I have update the twig fiddle with a work around though

| improve this answer | |
  • Thanks @Fyrebase. Apologies for my deleted comments as I was working off older versions of this page and the Twigfiddle example. So, yes, you have confirmed my finding. Thanks for the workaround - I haven't tried it yet in my code but I'm pretty sure it will do the job based on your demo. It is a pity because I like the include model for structuring my twig code - I find it cleaner than using blocks which I prefer for smaller bits of code. Perhaps I can get the workaround to fit my style. I didn't know about Twigfiddle, so that is a useful bit of info for me too. Thanks. – Jack McKenzie Aug 3 '16 at 6:39
  • Update: Yes it worked in my code. I just needed to add a |raw filter to deal with the HTML :) – Jack McKenzie Aug 3 '16 at 6:53
  • It's also worth noting that blocks don't work in the reverse either - you cannot define a block in the main template and override it inside an include. So as a general rule: don't use the {% block %} tag inside an include file. – Simon East Mar 17 '19 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.