Golang Web - HTML评论未呈现 [英] Golang Web - HTML Comments are not rendered

查看:110
本文介绍了Golang Web - HTML评论未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建golang web应用程序。我在呈现的html页面上发现了一些异常现象。我的所有html评论<! - - > 都没有呈现。



这是我的代码:

 <! - 准备面包屑 - > 

这是呈现的页面源代码:



。有些人建议使用 unescape ,但没有记录。我试过并产生了一些错误。 解决方案

html / template 包: template.HTML



因此,您可以将您的HTML注释标记为 template.HTML ,因此它们在执行模板时不会被转义或省略。



执行此操作的一种方法是为模板注册一个自定义函数,一个可以从你的模板中调用的函数,该函数接受一个 string 参数并将其作为 template.HTML 返回。您可以将所有HTML注释传递给此函数,因此,您的HTML注释将保留在输出中。



请参阅此示例:

  func main(){
t:= template.Must(template.New()。Funcs(template.FuncMap {
safe:func(s string)template.HTML {return template.HTML(s)},
})。Parse(src))
t.Execute(os.Stdout,无)
}

const src =`< html>< body>
{{safe<! - This is a comment - >}}
< div>一些< b> HTML< / b>内容< / DIV>
< / body>< / html>`

输出 Go Playground ):

 < HTML><身体GT; 
<! - 这是一条评论 - >
< div>部分< b> HTML< / b>内容< / DIV>
< / body>< / html>

因此,在注册我们的 safe()函数,将所有HTML注释转换为调用此 safe()函数并传递原始HTML注释的模板操作。



<转换此:

 <! - 一些HTML注释 - > 

至此:

  {{safe<! - 一些HTML注释 - >}} 

或者(无论你喜欢什么):

  {{<! - 一些HTML注释 - > ; |安全}} 

你很好。



注意:如果您的HTML注释包含引号(''),您可以/不得不像这样转义它:

  {{safe<! -  Some \HTML \comment  - >}} 

注意2:请注意,您不应该使用条件HTML注释因为这可能会破坏 html / template 包的上下文敏感转义。有关详细信息,请阅读


I'm building golang web application. I found some anomaly over the rendered html page. All of my html comments <!-- --> are not being rendered.

This is my code:

<!-- prepare the breadcrumbs -->
<ul class="breadcrumb" data-bind="foreach: viewModel.breadcrumbs">
    <!-- ko if: ($index() + 1) < len(viewModel.breadcrumbs()) -->
    <li>
        <a data-bind="attr: { href: href }">
            <i class="fa fa-home"></i> 
            <span data-bind="text: title"></span>
        </a>
    </li>
    <!-- /ko -->

    <!-- ko if: ($index() + 1) == len(viewModel.breadcrumbs()) -->
    <li class="active" data-bind="text: title"></li>
    <!-- /ko -->
</ul>

And this is the rendered page source:

Because of this issue, many of my knockoutjs code which is written using containerless control flow syntax goes crazy, it doesn't work at all.

What should I do to solve this? Thanks in advance

Edited #1

I found discussion on google groups about this issue. some people suggest to use unescape, but it's not documented. I have try it and produce some errors.

解决方案

There is a special type in the html/template package: template.HTML. Values of this type in the template are not escaped when the template is rendered.

So you may "mark" your HTML comments as template.HTML and so they will not be escaped or omitted during executing your template.

One way to do this is to register a custom function for your template, a function which can be called from your template which takes a string argument and returns it as template.HTML. You can "pass" all the HTML comments to this function, and as a result, your HTML comments will be retained in the output.

See this example:

func main() {
    t := template.Must(template.New("").Funcs(template.FuncMap{
        "safe": func(s string) template.HTML { return template.HTML(s) },
    }).Parse(src))
    t.Execute(os.Stdout, nil)
}

const src = `<html><body>
{{safe "<!-- This is a comment -->"}}
<div>Some <b>HTML</b> content</div>
</body></html>`

Output (try it on the Go Playground):

<html><body>
<!-- This is a comment -->
<div>Some <b>HTML</b> content</div>
</body></html>

So basically after registering our safe() function, transform all your HTML comments to a template action calling this safe() function and passing your original HTML comment.

Convert this:

<!-- Some HTML comment -->

To this:

{{safe "<!-- Some HTML comment -->"}}

Or alternatively (whichever you like):

{{"<!-- Some HTML comment -->" | safe}}

And you're good to go.

Note: If your HTML comment contains quotation marks ('"'), you can / have to escape it like this:

{{safe "<!-- Some \"HTML\" comment -->"}}

Note #2: Be aware that you shouldn't use conditional HTML comments as that may break the context sensitive escaping of html/template package. For details read this.

这篇关于Golang Web - HTML评论未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆