a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.39k stars 277 forks source link

Error generating code when nesting <a> and text in parent element. #998

Closed zachczx closed 2 weeks ago

zachczx commented 2 weeks ago

Hi, I encountered a minor bug (?) with Templ. Not a dealbreaker by any means, just made me puzzled for half an hour before realizing this was the cause when templ generate kept throwing errors.

This doesn't work - having an "a" tag immediately followed by text - throws error when generating code.

    <p>
        <a href="/" class="underline">Click here</a> if you're not redirected to the main page in 5s.
    </p>

This doesn't either.

    <p>
        <a href="/" class="underline">Click here</a><span> if you're not redirected to the main page in 5s.</span>
    </p>

This works fine.

    <p>
        <a href="/" class="underline">Click here if you're not redirected to the main page in 5s.</a>
    </p>

templ info output templ info (✓) os [ goos=windows goarch=amd64 ] (✓) go [ location=C:\Program Files\Go\bin\go.exe version=go version go1.23.3 windows/amd64 ] (✓) gopls [ location=C:\Users\z\go\bin\gopls.exe version=golang.org/x/tools/gopls v0.16.2 ]
(✓) templ [ location=C:\Users\z\go\bin\templ.exe version=v0.2.793 ]

joerdav commented 2 weeks ago

Hi @zachczx , thanks for the bug report. You have encountered a common footgun with templ, the issue is that the word after the a is if, here is the documentation for this behaviour: https://templ.guide/syntax-and-usage/statements#ifswitchfor-within-text

zachczx commented 2 weeks ago

Ah I'm so sorry, I read the docs and these lines many times, but I must've had a brain fart and skipped over the "if" after.

a-h commented 2 weeks ago

Did we not change the behaviour of the if parser to handle this case? I remember talking about it, maybe we overlooked making the change.

joerdav commented 2 weeks ago

I had the same thought @a-h ..

Maybe did we only implement it for one keyword to start with?

joerdav commented 2 weeks ago

https://github.com/a-h/templ/pull/777