Skip to content

Conflict between Twig language and DeletionInjection #182

@Kocal

Description

@Kocal

Tempest version

2.11.2

PHP version

8.4

Operating system

Linux

Description

In Twig, it's possible to add - to {{ variable }}, e.g.: {{- variable -}} to remove whitespaces from a side, see Whitespace control documentation, but it conflict with {- -} syntax from DeletionInjection.

The following code:

{% props variant = 'default' %}
<twig:Button variant="{{ variant }}" {{ ...attributes }}>
    {{- block(outerBlocks.content) -}}
</twig:Button>

is rendered like that:
Image

Ideally, {- and -} should not be understood as a deletion if there is a { or } preceding or following the deletion tokens.

As a current workaround, I can pass a custom Language implementation extending TwigLanguage, but which filters DeletionInjection from injections:

$language = new class extends TwigLanguage {
    public function getInjections(): array
    {
        return array_filter(parent::getInjections(), static function (Injection $injection) {
            return !$injection instanceof DeletionInjection;
        });
    }
};

Steps to reproduce

$code = <<<EOF
{% props variant = 'default' %}
<twig:Button variant="{{ variant }}" {{ ...attributes }}>
    {{- block(outerBlocks.content) -}}
</twig:Button>
EOF;
$language = 'twig';

$highlighter = new Highlighter();
$parsed = $highlighter->parse($code, $language);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions