Skip to content

Strange behavior with block function inside parent block #3321

Description

@hlecorche

Hello,

I found a strange behavior with all Twig versions.

{{ block('block1', 'theme.html.twig') }}`


{# theme.html.twig #}
{% extends 'base.html.twig' %}

{%- block block2 -%}
    BLOCK2THEME/
{%- endblock -%}



{# base.html.twig #}
{%- block block1 -%}
    BLOCK1BASE/
    {{- block('block2') -}}
{%- endblock -%}

{%- block block2 -%}
    BLOCK2BASE/
{%- endblock -%}

https://twigfiddle.com/jae42d

The result is the expected result : BLOCK1BASE/BLOCK2THEME/

  • block1: base.html.twig is used
  • block1 in base.html.twig calls block2: theme.html.twig is used (because block2 is overridden by theme.html.twig)

But I have a strange result if block1 is overridden and parent() is called :

{{ block('block1', 'theme.html.twig') }}`


{# theme.html.twig #}
{% extends 'base.html.twig' %}

{%- block block1 -%}
    {{- parent() -}}
    BLOCK1THEME/
{%- endblock -%}

{%- block block2 -%}
    BLOCK2THEME/
{%- endblock -%}



{# base.html.twig #}
{%- block block1 -%}
    BLOCK1BASE/
    {{- block('block2') -}}
{%- endblock -%}

{%- block block2 -%}
    BLOCK2BASE/
{%- endblock -%}

https://twigfiddle.com/jae42d/2

Expected result : BLOCK1BASE/BLOCK2THEME/BLOCK1THEME/
Result: BLOCK1BASE/BLOCK2BASE/BLOCK1THEME/

  • block1: theme.html.twig is used (because block1 is overridden by theme.html.twig)
  • block 1 in theme.html.twig calls parent: base.html.twig is used
  • block1 in base.html.twig calls block2: Unlike to the last example, theme.html.twig is not used. Why ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions