Skip to content

Support with keyword in block definitions #2435

Description

@mogsie

Today it's possible to provide "data" to blocks by wrapping the block definition in a {% with %}:

{% with { "foo": "ONE"} %}
  {% block one %}
    foo one: {{foo}}
  {% endblock %}
{% endwith %}

If the template is embedded elsewhere, and the block one overridden, it has access to the foo variable:

{% embed "something.twig" %}
  {% block one %}
    my foo one: {{foo}}
  {% endblock %}
{% endembed %}

The output of this would be my foo one: ONE

Here's a fiddle: https://twigfiddle.com/n9e1dr

This is a neat feature. It allows you to set up variables for each block, allowing the "user" of the template (where it is embedded) to access these variables.

I wonder if the following short-hand would make sense:

{% block one with { "foo": "ONE"} %}
  foo one: {{foo}}
{% endblock %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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