zoukankan      html  css  js  c++  java
  • goweb2-模板与渲染

    go语言的模板引擎

    text/template:文本模板引擎
    html/template:HTML文档
    作用:

    • 模板文件后缀:通常为.tmpl和.tpl(也可以是其他的),必须是UTF编码
    • 模板文件中使用{{}}包裹和标识需要传入数据
    • 传给模板的数据可用(.)来访问,如果数据复杂,可以通过{{.FieldName}}来访问它的字段。
    • 除{{}}包裹的内容外,其他内容均不做修改原样输出。

    解析模板文件

    func (t *Template) Parse(src string) (*Template, error)//从字符串中解析一个模板
    func ParseFiles(filenames ...string) (*Template, error)//根据文件名中解析一个模板
    func ParseGlob(pattern string) (*Template, error)//从正则匹配规则解析模板
    

    渲染:使用数据去填充模板

    func (t *Template) Execute(wr io.Writer, data interface{}) error
    func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error
    
    

    1传递字符串

    示例

    package main
    
    import (
    	"fmt"
    	"html/template"
    	"net/http"
    )
    
    func sayHello(w http.ResponseWriter, r *http.Request) {
    	// 解析指定文件生成模板对象
    	tmpl, err := template.ParseFiles("./hello.tmpl")
    	if err != nil {
    		fmt.Println("create template failed, err:", err)
    		return
    	}
    	// 利用给定数据渲染模板,并将结果写入w
    	tmpl.Execute(w, "沙河小王子")
    }
    func main() {
    	http.HandleFunc("/hello", sayHello)
    	err := http.ListenAndServe(":9090", nil)
    	if err != nil {
    		fmt.Println("HTTP server failed,err:", err)
    		return
    	}
    }
    
    

    编译运行后在网页打开

    2传递结构体

    定义模板

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Hello</title>
    </head>
    <body>
        <p>姓名 {{ .Name }}</p>
        <p>年龄 {{ .Age }}</p>
        <p>性别 {{ .Gender }}</p>
    </body>
    </html>
    

    解析模板+渲染模板

    //定义struct
    type User struct {
    	Name   string //首字母大写,才能被调用,小写是不能被调用的
    	Gender string 
    	Age    int
    }
    func sayHello2(w http.ResponseWriter, r *http.Request) {
    	//定义模板 hello.tmpl
    	//解析模板
    	tmpl, err := template.ParseFiles("./hello.tmpl")
    	if err != nil {
    		fmt.Println("err:", err)
    	}
    	//渲染模板
    	//结构体初始化
    	u1 := User{
    		Name:   "小王子1",
    		Gender: "男",
    		Age:    18,
    	}
    	tmpl.Execute(w, u1)
    }
    

    编译运行后在网页打开

    3传递多数据源

    本例为struct+map 其他类似

    定义模板

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Hello</title>
    </head>
    <body>
        <p>struct</p>
        <p>姓名 {{ .u1.Name }}</p>
        <p>年龄 {{ .u1.Age }}</p>
        <p>性别 {{ .u1.Gender }}</p>
    
        <p>map</p>
        <p>姓名 {{ .m1.name }}</p>
        <p>年龄 {{ .m1.age }}</p>
        <p>性别 {{ .m1.gender }}</p>
    </body>
    </html>
    

    解析模板+渲染模板

    //多数据源
    func sayHelloMapStruct(w http.ResponseWriter, r *http.Request) {
    	//定义模板 hello.tmpl
    	//解析模板
    	tmpl, err := template.ParseFiles("./hello.tmpl")
    	if err != nil {
    		fmt.Println("err:", err)
    	}
    	//渲染模板
    	//map初始化 map是通过key访问,key不需要大写
    	m1 := map[string]interface{}{
    		"name":   "小王子map",
    		"gender": "男",
    		"age":    18,
    	}
    	//结构体初始化
    	u1 := User{
    		Name:   "小王子struct",
    		Gender: "男",
    		Age:    18,
    	}
    	//模板渲染的数据源是第二个参数,所以前端 <p>性别 {{ .m1.gender }}</p>
    	tmpl.Execute(w, map[string]interface{}{
    		"u1": u1,
    		"m1": m1,
    	})
    }
    

    编译运行后在网页打开

    所有博客均为自己学习的笔记。如有错误敬请理解。
  • 相关阅读:
    Spring Cloud微服务实战 打造企业级优惠券系统 3-8 SpringBoot 单元测试
    Spring Cloud微服务实战 打造企业级优惠券系统 3-7 SpringBoot 异步任务(任务池)
    Spring Cloud微服务实战 打造企业级优惠券系统 3-6 SpringBoot定时任务
    Spring Cloud微服务实战 打造企业级优惠券系统 3-5 SpringBoot 配置注入的方式
    Spring Cloud微服务实战 打造企业级优惠券系统 3-4 SpringBoot配置文件加载顺序
    Spring Cloud微服务实战 打造企业级优惠券系统 3-3 SpringBoot自动配置原理
    Spring Cloud微服务实战 打造企业级优惠券系统 2-10 数据库连接池
    No spring.config.import set at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostPr
    Postman 上传文件
    springcloud minio 文件上传
  • 原文地址:https://www.cnblogs.com/tangtang-benben/p/15006577.html
Copyright © 2011-2022 走看看