golang html/template_html/css_WEB-ITnose
template包(html/template)实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出。本包提供了和text/template包相同的接口,无论何时当输出是HTML的时候都应使用本包。
main.go
package mainimport ( "html/template" "io/ioutil" "os" "time" "fmt")func main() { t := template.New("第一个模板").Delims("[[", "]]") //创建一个模板,设置模板边界 t, _ = t.Parse("hello,[[.UserName]]\n") //解析模板文件 data := map[string]interface{}{"UserName": template.HTML("<script>alert('you have been pwned')</script>")} t.Execute(os.Stdout, data) //执行模板的merger操作,并输出到控制台 t2 := template.New("新的模板") //创建模板 t2.Funcs(map[string]interface{}{"tihuan": tihuan}) //向模板中注入函数 bytes, _ := ioutil.ReadFile("test2.html") //读文件 template.Must(t2.Parse(string(bytes))) //将字符串读作模板 t2.Execute(os.Stdout, map[string]interface{}{"UserName": "你好世界"}) fmt.Println("\n", t2.Name(), "\n") t3, _ := template.ParseFiles("test1.html") //将一个文件读作模板 t3.Execute(os.Stdout, data) fmt.Println(t3.Name(), "\n") //模板名称 t4, _ := template.ParseGlob("test1.html") //将一个文件读作模板 t4.Execute(os.Stdout, data) fmt.Println(t4.Name())}//注入模板的函数func tihuan(str string) string { return str + "-------" + time.Now().Format("2006-01-02")}
<!DOCTYPE html><html><head> <title>template</title></head><body>hello {{.UserName}}<br></body></html>
<!DOCTYPE html><html><head> <title>template</title></head><body>hello {{.UserName}}<br>{{tihuan .UserName}}</body></html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...
