继续-不呈现HTML注释 [英] Go - HTML comments are not rendered

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

问题描述

我正在构建Go Web应用程序.我在呈现的html页面上发现了一些异常.我所有的HTML注释<!-- -->都突然没有被渲染.我的猜测是因为我使用了go版本(只是更新为更高版本),因为在我更新它之前还可以.

I'm building go web application. I found some anomaly on the rendered html page. All of my html comments <!-- --> are suddenly not being rendered. My guess it's because the go version I used (just updated to higher version), because it was fine before I updated it.

这是我的代码:

<!-- 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>

这是渲染的页面源:

由于这个问题,我的许多使用无容器控制流语法编写的KnockoutJS代码都发疯了,根本没有用.

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

我应该怎么做才能解决这个问题?预先感谢

What should I do to solve this? Thanks in advance

推荐答案

html/template 软件包: template.HTML .呈现模板时,不会转义模板中此类型的值.

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.

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

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

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

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.

请参见以下示例:

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>`

输出(在转到操场上尝试):

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

因此,基本上,在注册我们的safe()函数之后,将所有HTML注释转换为调用此safe()函数并传递原始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.

转换此:

<!-- Some HTML comment -->

对此:

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

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

Or alternatively (whichever you like):

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

你很好.

注意::如果您的HTML注释包含引号('"'),则可以/必须像这样对它进行转义:

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

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

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

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.

这篇关于继续-不呈现HTML注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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