trivago / prettier-plugin-twig-melody

Code formatting plugin for Prettier which can handle Twig/Melody templates
Apache License 2.0
157 stars 35 forks source link

melody-parser does not like dynamic elements #53

Open illepic opened 4 years ago

illepic commented 4 years ago

I know this isn't necessarily related to this project, but attempting something like this

{% set elementType = 'button' %}
<{{elementType}}>I'm a button</{{elementType}}>

throws the following error:

Error: ERROR: Expected element start
(code with arrows)

Expected an element to start
mtrischetta commented 3 years ago

This is becoming a blocker for my team because we need dynamic elements and want to run prettier as a pre-commit hook. Is there any more info I can provide to help?

titouanmathis commented 3 years ago

We use a custom Twig tag as a workaround for anyone interested:

{# Twig template #}
{% set tag = 'p' %}
{% html_element is tag with { id: 'lorem-ipsum' } %}
  Lorem ipsum
{% end_html_element %}

{# HTML output #}
<p id="lorem-ipsum">
  Lorem ipsum
</p>
mtrischetta commented 3 years ago

Thanks for the workaround @titouanmathis - we need to use vanilla Twig in our codebase and ended up implementing this from scratch, and ignoring only this file from Prettier

html_element.twig

<{{ tag }} {{ attributes }}>
    {{content}}
</{{ tag }}>

.prettierignore path/to/twig/html_element.twig