Search code examples
gohtml-escape-charactersgo-html-template

How to stop go html/template from escaping a path (tried .HTML .JS etc.)


I am using the html/template functionality to assemble a page and on of the variables I'm supplying to the template is URI in the form "/some/path/etc" which is used as a parameter to a JS function called in a onClick="..".

No matter what, the string used in this configuration will be escaped with backslashes : "\/some\/path\/etc"

As you can see in the playground example below, I tried all the .HTML(), .JS() etc. functions but nothing seems to stop the escaping.

See Go Playground example here: https://play.golang.org/p/2gdghTpQHKP

How can I get this URI "as is" into the template result?


Solution

  • Thanks to mkopriva for his comment.

    As far as I could see there is no way (as mkopriva mentioned) to handling a HRML attribute value fragment in a Go HTML template. So the options are:

    1. Leave it as is (it seems that at least in my use case the URI even works in the further processing with the escaped forward slashes)
    2. first concatenate the complete attribute, so that the "HTML Attribute" way will accept it
    3. Write a construction function that takes parts and assembles the final attribute value inside the template execution
    4. Hardcode the value in some form