Duplicating code if wrapping start and end HTML tag in separate `if` statements
The following minimal example code ...
{{ if .url }}<a href="{{ .url }}">{{ end }}
<span>Content</span>
{{ if .url }}</a>{{ end }}
... duplicates to the following upon format:
{{ if .url }}<a href="{{ .url }}"></a>{{ end }}
<span>Content</span>
{{ if .url }}<a href="{{ .url }}">{{ end }}
<span>Content</span>
{{ if .url }}</a>{{ end }}
My current workaround is this:
<!-- prettier-ignore -->
{{ if .url }}<a href="{{ .url }}">{{ end }}
<span>Content</span>
<!-- prettier-ignore -->
{{ if .url }}</a>{{ end }}
Hey there, sadly this is notoriously hard to fix as currently the template is parsed as a tree. The go formatter invokes the prettier html formatter for the contained text, but that one struggles when there are missing tags etc.
I'll try to find a solution to work around this. Thanks for reporting the issue.
@schnerring sorry, I have this nearly fixed – but until the HTML formatter has an option to disable auto-closing tags, I'll have to keep this open.
Related prettier issue: https://github.com/prettier/prettier/issues/5665
Would it be possible to error & link to this issue instead of duplicating code?
I'm pretty sure this pattern isn't allowed in JSX.. it seems reasonable to not allow it until there's a workaround.
It moght be worth looking at how handlebars was implemented 🤔
@schnerring sorry, I have this nearly fixed – but until the HTML formatter has an option to disable auto-closing tags, I'll have to keep this open.
As I've mentioned, I have a workaround, so no pressure. 😄 Thanks for looking into this!