symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
827 stars 299 forks source link

[TwigComponent] two levels of nesting do not work #1954

Closed tito10047 closed 2 months ago

tito10047 commented 3 months ago

Hi, I have this situation:

{# components/Body.html.twig #}
<body{{ attributes }}>
    <main class="app-main">
        {% block content %}{% endblock %}
    </main>
</body>
{# base.html.twig #}
<!DOCTYPE html>
<html>
<head>
</head>
<twig:Body>
    {{ block(outerBlocks.body) }}
</twig:Body>
</html>
{# layout.html.twig #}
{% extends "base.html.twig" %}

{% block body %}
    {{ block(outerBlocks.container) }} {# this not work #}
    {% block container %}{% endblock %} {# this also not work #}
{% endblock %}
{# index.html.twig #}
{% extends "layout.html.twig" %}

{% block container %}
    This block is not rendered
{% endblock %}

When i want render index.html.twig then container block is not rendered

Any idea how to render this block ? Thanks

smnandre commented 3 months ago

First thing: I'm not sure you should create a Twig component for your entire body.

In your layout.html.twig, you use a block "body" than you never defined... The twig:Body component is a .. component, and the fact it could work calling it body is nothing documented, and that could change at any time.

Is there any reason here that prevent you to use standard twig blocks ?

tito10047 commented 3 months ago

Its not about component Body. Its only simulation of problem. But In my aplication have similar component Layout where I want have logic which layout I want use. So aproach is similar and problem is same.

And why im using components? I want build universal template for all my projects. I think components are suitable for this.

So should I take it as saying that this cannot be done with twig components?

smnandre commented 3 months ago

Its not about component Body.

Just saying you have no outer blocks in this template. so this wont work.

<twig:Body>
    {{ block(outerBlocks.body) }}
</twig:Body>

So should I take it as saying that this cannot be done with twig components?

I'm saying this is much easier with twig regular blocks / extends / embeds... Twig is the perfect tool to build layout structures with inheritence, etc... TwigComponents are made to have PHP-based capabilities in an isolated scope.

tito10047 commented 2 months ago

But i like props in twig comnponents. With it I can simply modify the view. I dont need to remmember all classes, just set the props and everything in template is hapening.

But thanks for your time, I will switch to classic blocks