Strip HTML comments before processing template

205 views
Skip to first unread message

atilkan

unread,
Dec 30, 2024, 7:23:47 PM12/30/24
to golang-nuts
Hi,
I am trying to add html comment (yes, they will be removed). I believe it strips the comments after processing the template. If you put non-existing property inside html comments, it throws error.
Maybe better to strip comments before processing the template?

`<!-- <input value='{{.nonExistingData}}'> -->`

I would paste the whole error but you have to search needle in haystack. Basically says "can't evaluate field nonExistingData".


Have a nice day.

Jon Perryman

unread,
Dec 30, 2024, 11:33:39 PM12/30/24
to atilkan, golang-nuts
"can't evaluate field nonExistingData" looks like a javascript error message because it says evaluated. HTML would not evaluate stuff. The outer tics also make me think javascript. I inserted the statement (with & without outer tics) but don't get an error message on the console.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/10547051-0324-4257-a81d-33644d92bca5n%40googlegroups.com.

Emrah ATILKAN

unread,
Dec 31, 2024, 6:34:53 AM12/31/24
to Jon Perryman, golang-nuts
I don’t think it is JS error. This is a Go runtime error. Not from browser console, from terminal. 

Kurtis Rader

unread,
Dec 31, 2024, 7:32:19 AM12/31/24
to atilkan, golang-nuts
You should always include the full error message (and backtrace if available) as well as telling us the relevant version of Go you used to compile your program and the particular package you believe is the source of the problem. A minimal reproducible example is also a good idea if feasible.

In this case we're guessing you're using https://pkg.go.dev/html/template but we shouldn't have to guess. Jon guessed that the error is from a Javascript interpreter, and not a Go stdlib package, because of the lack of context and the wording of the partial error message you gave us. I'm guessing the error is from src/text/template/exec.go:

s.errorf("can't evaluate field %s in type %s", fieldName, typ)

Note that html/template delegates the actual substitution to text/template.

The documentation for html/template (https://pkg.go.dev/html/temp...@go1.23.4) explicitly states that the HTML type should not contain comments:

HTML encapsulates a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. The outputs of a sound HTML sanitizer and a template escaped by this package are fine for use with HTML.

Whether that package comment is relevant is unclear since you didn't provide enough information for us to do more than make educated guesses.

I took the first example program at https://pkg.go.dev/html/temp...@go1.23.4 and inserted your string as the first line of the "const tpl = ..." definition. That resulted in this runtime failure:

2024/12/30 23:28:23 template: webpage:2:21: executing "webpage" at <.nonExistingData>: can't evaluate field nonExistingData in type struct { Title string; Items []string }

Does that look like the error you're seeing?


On Mon, Dec 30, 2024 at 11:23 AM atilkan <emraha...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/10547051-0324-4257-a81d-33644d92bca5n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Jon Perryman

unread,
Dec 31, 2024, 7:51:30 AM12/31/24
to Emrah ATILKAN, golang-nuts
Sorry, I was thinking you were talking about the html template tags. HTML comments <!--  --> are processed like any other html tag. Does one of the following produce the results you are looking for:

{{ `<!-- <input value='{{.nonExistingData}}'> -->` }}
{{ /* `<!-- <input value='{{.nonExistingData}}'> -->`  */ }}



Kurtis Rader

unread,
Dec 31, 2024, 8:20:37 AM12/31/24
to atilkan, golang-nuts
Argh! I just realized my reference to the comment about the html/template "HTML" type handling of comments isn't really relevant. The issue is that the html/template package doesn't inhibit substitution inside a semantic HTML comment. It does elide comments from the generated output but does attempt to perform substitutions within the HTML comment. This doesn't surprise me but it also wasn't obvious to me from a quick reading of the package documentation. Perhaps the documentation would benefit from the addition of some clarifying text on this point. You have to actually convert the HTML comment into a literal string. For example, changing

<!-- <input value='{{.nonExistingData}}'> -->

into

{{ "<!-- <input value='{{.nonExistingData}}'> -->" }}

atilkan

unread,
Dec 31, 2024, 11:25:46 AM12/31/24
to golang-nuts
Yep that would fix the issue. But i am thinking those stripped html comments should not be evaluated. That seems odd to me.

Jon Perryman

unread,
Dec 31, 2024, 4:48:55 PM12/31/24
to Kurtis Rader, atilkan, golang-nuts
More to the point, why is atilkan coding `<!-- <input value='{{.nonExistingData}}'> -->` an HTML comment when golang templates is a preprocessor language.that is going to strip HTML comments? If he is retaining code in the template for future reference, then why not use a template comment {{ /* */ }} instead of an HTML comment. Anyone familiar with PHP is accustomed to dealing with this situation where HTML, Javascript & more has a conflict with the PHP language (e.g. passing quotes thru PHP).

Mike Schinkel

unread,
Jan 1, 2025, 9:44:25 PMJan 1
to atilkan, golang-nuts
If it stripped HTML comments then it would not be possible to generate output that contains wanted comments. Not all generated code is destined to be delivered to a renderer. Some might be targeting developers who need to modify it manually and for whom comments would be desired, including comments containing dynamically generated data.

Better to use template parser-compatible comments for your use-case, as Jon Perryman mentioned, than to limit what can be generated by the package

-Mike

Brian Candler

unread,
Jan 2, 2025, 8:31:12 AMJan 2
to golang-nuts
On Wednesday, 1 January 2025 at 21:44:25 UTC Mike Schinkel wrote:
If it stripped HTML comments then it would not be possible to generate output that contains wanted comments.

But it *does* strip comments, doesn't it?

Jon Perryman

unread,
Jan 2, 2025, 6:01:12 PMJan 2
to Brian Candler, golang-nuts
You are ignoring the past, future and features of GOLANG HTML when you say HTML comments are removed as a default. I'm guessing the original design transmitted HTML comments and they just suppressed them. At some point, I'm guessing they will add a GOLANG HTML template switch that transmits HTML comments for things like debugging at the browser where variable substitution could be useful in HTML comments. If they eliminated processing, implementing such a switch could present a challenge.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages