1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
KaMaCh
  • 19
  • 3
  • Possible duplicate of https://stackoverflow.com/q/36507836/13860 – Jonathan Hall Jul 05 '19 at 11:40
  • The possible duplicate above asks how to get rid of the quotes of the passed string, which in my case is not problematic (I need the unescaped content of the string). Also I have already tried using template.JS() as mentioned to no avail.thx – KaMaCh Jul 05 '19 at 13:08
  • AFAICT there's no support for handling an HTML attribute value's *fragments*, which is what your URI is in the context you're trying to use it. The closest supported type of thing would be the [HTML attribute](https://golang.org/pkg/html/template/#HTMLAttr), ie *attribute="value"*. So to render the attribute the way you need to you need to provide the whole attribute to your `htmlattr` function. If you don't want to hardcode the attribute you could always write a func that constructs it. https://play.golang.org/p/o2JJF9op8dK – mkopriva Jul 05 '19 at 14:07

1 Answers1

0

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
KaMaCh
  • 19
  • 3