How to let html/template not escape HTML tags in Golang

Source: Internet
Author: User
Tags define function
This is a creation in Article, where the information may have evolved or changed.

Recently developed a simple Haproxy load Balancing task management system using Golang's Net/http and html/template (see the article on building high-availability load balancing components and caching DNS).

Htmp/template the HTML tags in the string are escaped by default when rendering the page template, but sometimes we don't want to escape HTML tags, as shown in the example:

Figure 1, "Ip:port list (one line)" and "description" the contents of the two input boxes are separated from the rows, and in \n Figure 2, the two sections are shown in the table's "Back-end machine list" and "description" columns, but the rows and rows are actually
delimited , then replace the data in the string with the data before it is stored in the database or after it is fetched from the database \n
. If you pass the replaced data to the template as a string type,
the effect of the label rendering is
text rather than wrapping.

There are two ways to avoid html/template escaping HTML tags:

1. Convert the string type data into template.HTML a type and then pass in the template for rendering:

LTI := Listentaskinfo{    Seq:      seq,    Id:       Row.Id,    Servers:  Template.HTML(Strings.Join(Strings.Split(Row.Servers, "-"), "
")), Vip: appconf.Vip, Vport: Row.Vport, Comment: Template.HTML(Strings.Join(Strings.Split(Row.Comment, "\ n"), "
")), Logornot: Row.Logornot, DateTime: Row.DateTime,}

2. html/template allows you to add a handler function to the template variable as needed, which can be further processed by the template when parsing the template, such as:

{. | html}}

html/templateThere seems to be no built-in function that doesn't escape HTML tags, but provides an interface that lets us customize such functions as needed. Then we can customize a function-the template variable is converted to a type when the template is parsed template.HTML , such as (the example is from how to unescape the Text in A golang Html template):

func unescaped (x string) Interface{} { return Template.HTML(x) }func rendertemplate(W http.Responsewriter, Tmpl string, View *Page) {    T := Template.New("")      T = T.Funcs(Template.Funcmap{"unescaped": unescaped})    T, Err := T.Parsefiles("View.html", "Edit.html")    Err = T.executetemplate(W, Tmpl + ". html", View)    if Err != Nil {        http.Error(W, Err.Error(), http.Statusinternalservererror)    }}

This code makes it possible for template parsing to use the unescaped function to convert the template variable x into a template.HTML type, the key being the following two sentences:

//define function unescaped func unescaped (xstring) Interface {}{returntemplate. } HTML (x) }//Register unescapedt=T in template Object T. Funcs (template. Funcmap {"unescaped":unescaped})

In this way, you can use the unescaped function in the template, such as:

{{printf'%s '. } Body | unescaped }}//[]byte{{. Body | unescaped }}//string

Implementations do not escape HTML tags, essentially, the two methods are the same, except that they are converted to a type before the string is passed into the template template.HTML , and the other is converted when the string is parsed after it is passed into the template.

In addition template.HTML to types, text/template template.JS data types are defined, and template.CSS so on.


    • Html/template
    • How to Unescape Text in A golang Html Template
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.