Twig {% extends %} does not pass variables - twig

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 %}

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 %}

Multiple blocks in Grav cms twig template

I am making a Grav template from a static html. This is a one-pager, so I am using modular templates and content. The problem is that I have a gallery, and my clicking on an image, a modal pops up. The code for the modals are places outside the container I am using for the image gallery.
Is there a way to have a twig modular template creating output code two different places on the base template? I have tried using multiple blocks in the modular template, but in the final output all code are places inside the image gallery container? My templates have multiple level nesting just to make it a little bit easier:
main
Listing of categories
Listing of images for each category
base.html.twig (simplified):
{% block body %}
{% block modal %}{% endblock %}
{% block content %}{% endblock %}
{% endblock %}
modular.html.twig:
{% extends 'partials/base.html.twig' %}
{% block content %}
{{ page.content }}
{% for module in page.collection() %}
<div id="{{ _self.pageLinkName(module.menu) }}">
{{ module.content }}
</div>
{% endfor %}
{% endblock %}
category-list.html.twig (simplified):
{% block content %}
{% for module in page.collection() %}
{{ module.content }}
{% endfor %}
{% endblock %}
content template (simplified):
{% block modal %}
{{ page.titel }}
{% endblock %}
{% block content %}
{{ page.content }}
{% endblock %}
My problem: rendering the block modal from the content template in the block Modal in the base template. Currently the block modal is rendered in block content.
Any ideas?
Thanks

twig: Overriding blocks defined in embed tag

Is there a way to override blocks defined inside and embed tag inside the parent template in a child template.e.g: I have three templates: a, b and c. a embeds b and c extends a.
{# a.html.twig #}
{% embed b.html.twig %}
{% block content %}
laksjflkj
{% block placeholder %}
I want to override this template in c, it is actually defined here and has nothing to do with b
{% endblock placeholder %}
{% endblock content %}
{% endembed %}
{# b.html.twig #}
{% block content %}
blahblah
{% endblock %}
{# c.html.twig #}
{% extends 'a.html.twig' %}
{% block placeholder %}
let's override the block defined inside a
{% endblock placeholder %}
How do I override the placeholder block inside c.html.twig?
You have to create a new file called d.html.twig, which extends c.html.twig and override your placeholder block like this :
{# d.html.twig #}
{% extends 'c.html.twig' %}
{% block placeholder %}
Overrided !
{% endblock placeholder %}

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 %}

Access block from included file

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

Resources