go template继承 引用 ParseGlob 匹配符加载template Execute和ExecuteTemplate 更改模板名字

  • {{template “js.html”}}引用js.html,不需要写路径。只要ParseGlob或者parsefiles能找到即可。可以读取不同文件夹下的同名文件,不报错,具体用哪个未知。
  • 读取的模板名默认为当前文件名,如template\test1\block.tmpl的模板名是block.tmpl,但如果在block.tmpl里用{{define “a/b/c.html”}}{{end}}包裹整个模板的内容,可以重命名模板名为a/b/c.html。代码见最下方
  • 模板a里写{{block “part0” .}}{{end}},b引用a,b里写{{define “part0”}}part0的html内容{{end}}填充a的part0
  • {{block “part0” .}}{{end}}貌似和 {{template “part0”}}效果一样,不知道有其他什么用
  • template.ParseFiles读取了1个文件,用Execute和ExecuteTemplate都行。读取了多个文件,用ExecuteTemplate。原因不明。建议全部用ExecuteTemplate。参考https://qiita.com/tetsuzawa/items/b2dc94d7bdc906268534
  • tmpl,err:=template.ParseGlob(“./template/※※/※”),匹配./template下所有文件夹(递归)的所有文件
  • tmpl,err:=template.ParseGlob(“./template/test※/※”),匹配./template下名字以test开头的文件夹(非递归)的所有文件
  • tmpl,err:=template.ParseGlob(“./template/※/※/※”),匹配./template下里两层以内所有文件夹(非递归)的所有文件
  • tmpl,err:=template.ParseGlob(“./template/※.tmpl”),匹配./template下所有.tmpl文件

上面的代码※是匹配符*,※※表示任意层级的子文件夹,※表示单层或任意字符

//template\test1\block.tmpl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {{template "js.html"}}
    <title>Document</title>
</head>
<body>
    <div>part1</div>
    {{template "part0"}}
    <div>part2</div>
    {{block "part0" .}}{{end}}
    <div>part3</div>
</body>
</html>

//template\test1\useblock.tmpl
{{template "block.tmpl"}}

{{define "part0"}}
    <h1>this is part0 </h1>
{{end}}
//template\test2\js.html
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
//a1.go
package main

import (
	"html/template"
	"log"
	"net/http"
	// "github.com/gin-gonic/gin"
)

func main() {
	http.HandleFunc("/a1", a1)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Println("HTTP server failed,err:", err)
		return
	}

}

func a1(w http.ResponseWriter, r *http.Request) {
	tmpl,err:=template.ParseGlob("./template/**/*.*")
	// tmpl,err:=template.ParseGlob("./template/test*/*")
	// tmpl,err:=template.ParseGlob("./template/*/*/*")
	// tmpl,err:=template.ParseGlob("./template/**/*.tmpl")
	if err != nil {
		log.Println("create template failed, err1:", err)
		return
	}
	err=tmpl.ExecuteTemplate(w,"useblock.tmpl",nil)
	if err != nil {
		log.Println("create template failed, err2:", err)
		return
	}
}
访问a1页面,内容如下
part1
this is part0
part2
this is part0
part3

遇到的bug:
为了测试建了test2文件夹,一读取就报错The handle is invalid。删除后重新建一个就没bug了,原因不明。

重命名模板名

//template\test1\block.tmpl
{{define "a/b/c.html"}}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        {{template "js.html"}}
        <title>Document</title>
    </head>
    <body>
        <div>part1</div>
        {{template "part0"}}
        <div>part2</div>
        {{block "part0" .}}{{end}}
        <div>part3</div>
    </body>
    </html>
{{end}}
//template\test1\useblock.tmpl
{{template "a/b/c.html"}}

{{define "part0"}}
    <h1>this is part0 </h1>
{{end}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值