Access block from included file - twig

I want to access a block inside an included file.
Something like:
{#template A #}
some HTML
{% block blockA %}
{% endblock blockA %}
some HTML
{% block blockB %}
{% endblock blockB %}
some HTML
{#template B #}
{% extends A %}
{% block blockA %}
{% include C %}
some HTML
{% endblock %}
{#template C #}
{% block blockB %}
some HTML
{% endblock%}
All the HTML that i put on template C didnt go inside "blockB".

I think you are looking for embed instead of include.
Twig Documentation

Related

Twig does not replace blocks from child template

I have a simple setup. A parent template with some {% block ... %} elements and a child template which defines those elements.
Oddly (to me as a beginner), no {% block ... %} element is recognized.
The code:
{# main.twig #}
Hello {% block greeting %}{% endblock %}
{% block body %}{% endblock %}
{# child.twig #}
{% extends "main.twig" %}
{% block greeting %}friend{% endblock %}
{% block body %}Some text{% endblock %}
{% block footer %}{% endblock %}
This is already not working
Here a (not working) example of the exact problem:
https://twigfiddle.com/p2i0ix
In the example I also added a third level:
{# child_extend.twig #}
{% extends "child.twig" %}
{% block footer %}Footer text{% endblock %}
As you can see in the example, no {% block ... %} gets output.
Naturally I want all my blocks to get replaced and shown in the respective templates.
I am surely missing something blindingly obvious.
Thank you in advance for your help.
In your twigfiddle, check the file you want to test as the main template.
If you want to test child.twig, check this as the main template.
The other problem is that the block foot is not the main file, from which it does not appear.
either you put it in the main file, or you can do something like that:
{# child.twig #}
{% extends "main.twig" %}
{% block greeting %}friend{% endblock %}
{% block body %}Some text
{% block footer %}{% endblock %}
{% endblock %}
It's just for the example, the better is to put the block footer in the main file and only fill in the file child_extend.twig
{# main.twig #}
Hello {% block greeting %}{% endblock %}
{% block body %}{% endblock %}
{% block footer %}{% endblock %}
-------------------------------
{# child.twig #}
{% extends "main.twig" %}
{% block greeting %}friend{% endblock %}
{% block body %}Some text{% endblock %}
-------------------------------
{# child_extend.twig #}
{% extends "child.twig" %}
{% block footer %}Footer text{% endblock %}

Twig, is it possible to override an tag atribute of parent template from a child template?

I have two templates, a parent template and a child template.
I want know if its possible to add 'properly' a class to a tag in the parent template from the child template ? and if yes, how ?
By example, if I have this parent.html.twig file :
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
And this child.html.twig file :
{% extends 'parent.html.twig' %}
{% block body %}
{# ... #}
{% endblock %}
From the child.html.twig file, can I add a class to the body tag ? and how ?
Thanks for help :)
You should modify the parent template adding a block, as example:
parent.html.twig
<body {% block bodyclass %}{% endblock %}>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
and use in the child:
child.html.twig
{% extends 'parent.html.twig' %}
{% block bodyclass %}class="child-class"{% endblock %}
{% block body %}
{# ... #}
{% endblock %}
You can try in this twigfiddle

Twig {% extends %} does not pass variables

I use Kohana with twig and extend a template within a template. Param, which is available in my template is not passed to the extended template. How to pass all or at least that value to the extended template?
{% extends 'customer/main' %}
{% block content %}
Will show the content of my_param:{{ my_param }}
{% endblock content %}
File: customer/main.html
{% extends 'main' %}
{% block middle %}
Will NOT show the content of my_param:{{ my_param }}
{% block content %}{% endblock content %}
{% endblock middle %}

Pass include path from variable with Twig?

I need to include child-1.twig and child-2.twig in component.twig, and include component.twig in page.twig.
In my page.twig:
{% set items = [
'{% include "child-1.twig" %}',
'{% include "child-2.twig" %}'
] %}
{% include "component.twig" with items %}
In component.twig:
<div class="component">
{% for item in items %}
{{ item }}
{% endfor %}
</div>
The complexity comes from the fact that I cant modify component.twig, only page.twig. My code above would work if {% include "child-1.twig" %} and {% include "child-2.twig" %} were rendered but instead they are printed onto the page as a string of text.
Can I do something similar to my approach but make the child include actually run?
Can I suggest you add an empty block in that file (component.twig)
{% block includes %}{% endblock %}
Then you will be able to do this:
{% embed "component.twig" with items %}
{% block includes %}
{% include "child-1.twig" %}
{% include "child-2.twig" %}
{% endblock %}
{% endembed %}

How to reuse block in an included template using twig

I have a userDashboard.html.twig template like this:
{% extends "AcmeDemoBundle::base.html.twig" %}
{% block content %}
<h1>Name</h1>
{% endblock %}
{% include "AcmeDemoBundle::statistics.html.twig" %}
The controller call this template(userDashboard).
And a statistics.html.twig where I try to override or extend the content block:
{% extends "AcmeDemoBundle::userDashboard.html.twig" %}
{% block content %}
{{ parent() }}
Something
{% endblock %}
My problem is that I can't do this way. Can somebody recommend a solution?
There's embed which basically lets you include templates while overriding some of their blocks:
{% embed "AcmeDemoBundle::userDashboard.html.twig" %}
{% block content %}
{{ parent() }}
Something
{% endblock %}
{% endembed %}

Resources