-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html/template: script type attribute: {{if}} branches end in different contexts #57136
Comments
Working as intended, from the documentation of the error:
|
The example in the documentation has an {{if}} outside the tag and its {{end}} inside the tag (as far as we can tell). Hence the "different context". In my case, both {{if}} and {{end}} are inside the tag. {{if} branches are allowed and are useful around attributes. How else would you place attributes conditionally in a tag? The problem is with one specific attribute ("type") in the <script> element. I do not get the error for other <script> attributes or for a "type" attribute in other elements. |
I am looking at the code (escape.go) now and I see the special treatment of the "type" attribute inside a <script> tag: |
This no longer affects me. If I want to add type="module" conditionally, I can do it differently. This works, for example, and it is better because the type attribute cannot have arbitrary values:
It turns out I don't need this conditional at all. JavaScript modules are imported differently in HTML than other JavaScript. |
The following template:
causes the following error when executed (regardless of the value of .type):
This behavior is specific to the script type attribute. It executes as expected if "type=" is replaced with "xtype="
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?debian/11 amd64
go env
OutputWhat did you do?
https://go.dev/play/p/F4f3aMmjMVw
What did you expect to see?
<script type="module"></script>What did you see instead?
html/template:test:1:13: {{if}} branches end in different contexts: {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementNone }, {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementScript }
The text was updated successfully, but these errors were encountered: