a-h / templ

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

proposal: Go comments within HTML tags #866

Open romshark opened 1 month ago

romshark commented 1 month ago

In addition to #79, I'd like to propose // comments within HTML tags, such as:

<form
  class="flex"
  // This comment here explains why the following attribute is necessary,
  // namely to prevent submit when hitting the enter key.
  // But templ won't allow this comment to be here and throws syntax errors.
  action="javascript:void(0)"
>

image

Current Workaround

Currently, the comments must be put above the HTML tag, which is not always convenient.

// Use action="javascript:void(0)" to prevent submit on enter
<form
  class="flex"
  action="javascript:void(0)"
>

Since // is not a valid token inside an HTML tag IMHO it should be possible to allow Go comments within tags that aren't included in the compiled HTML.

bastianwegge commented 1 month ago

I would also love this. We are currently developing libraries for internal use and I'd also like to add some comments between alpine relevant directives:

<div
  // Alpine data initialization
  // makes this
  // does that
  x-data="{
    ...
  }"
  // Alpine component initialization
  // makes this
  // does that
  x-init="
    ...
  "
  @keydown.escape.window="commandOpen = false"
  class="relative z-50 w-auto h-auto"
>
...
</div>

We do lots of these and I'd like to add some comments to make things clearer.