Skip to content

Twig: Splitting HTML Attribute with an IF-clause will cause deletion! #621

Open
@christophhapple

Description

@christophhapple

Source

<div class="always-present{% if withCollapse %}with-collapse" data-toggle="collapse"{% else %}no-collapse"{% endif %}>
<p>Thingy here, possibly with collapse attached.</p>
</div>

Result after beautify

<div class="always-present{% if withCollapse %}with-collapse" data-toggle="collapse" {% else %} no-collapse="no-collapse"

Problem

The {% endif %} and everything afterwards just get stripped away!

Story

I ran into this issue with titles that only sometimes have a collapsed attached.
To achieve different styling, I add different classes depending on some logic.
Trying to be clever, I also wanted to add other HTML attributes using the same logic.

When wanting a collapse, I add the class with-collapse to the class-attribute.
Also, I add the data-toggle="collapse" attribute.
When not wanting a collapse, I add the class no-collapse to the class-attribute.
The data-toggle-attribute is omitted in this case.

I realize this is messy HTML to begin with and it might be bad practice to use if-clauses to split HTML attributes the way I did.
PHPStorm already warns me that not all tags are claused since it somehow cannot handle the entanglement of attributes and if-clauses, too.
But still I think just deleting stuff is not a sane way to handle this ;)


Please excuse me if you find the bug report lacking or peculiar somehow, this is my first time ;)


PS: Turning the logic around makes no difference.

Source

<div class="always-present{% if noCollapse %} no-collapse"{% else %}with-collapse" data-toggle="collapse"{% endif %}>
<p>Thingy here, possibly with collapse attached.</p>
</div>

Result

<div class="always-present{% if noCollapse %} no-collapse" {% else %} with-collapse" data-toggle=" collapse="collapse"

Edits for layout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions