Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tito10047 opened this issue Jul 3, 2024 · 4 comments
Closed

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

tito10047 opened this issue Jul 3, 2024 · 4 comments

Comments

@tito10047
Copy link

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

@tito10047 tito10047 changed the title [TwigComponents] two levels of nesting do not work [TwigComponent] two levels of nesting do not work Jul 3, 2024
@smnandre
Copy link
Collaborator

smnandre commented Jul 3, 2024

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
Copy link
Author

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
Copy link
Collaborator

smnandre commented Jul 6, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants