doufan1363
doufan1363
2019-07-05 10:51

如何停止转义html / template的路径(尝试使用.HTML .JS等)

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtny30176 dtny30176 2年前

    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
    点赞 评论 复制链接分享