Go lang templates: always quotes a string and removes comments

て烟熏妆下的殇ゞ 提交于 2019-12-07 13:00:54

问题


This Go code always quotes a string: http://play.golang.org/p/8k4s8dv2PE in the template - you can see results. How can I generate var currentUser = null? Note it also removes all comments from the code! How is it tuned up? This question is continuation of my Go: quoted string in templates.


回答1:


The html/template package is expressly designed to escape values. In your case, you're trying to pass JavaScript code in, rather than a simple value. You can accomplish this by changing the type of UserEmail to that of template.JS. This type wraps a string and expresses the intent that this value is valid JavaScript and should be substituted directly (without quotes) when used in a JavaScript context.

This version of the code does precisely that: http://play.golang.org/p/aNGnFMyY1O



来源:https://stackoverflow.com/questions/14226416/go-lang-templates-always-quotes-a-string-and-removes-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!