随笔 - 21  文章 - 0  评论 - 0  阅读 - 6157

go 模板

template.ParseFiles()
实现: func ParseFiles(filenames ...
string) (*Template, error) { return parseFiles(nil, readFileOS, filenames...) }
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
    return parseFiles(t, readFileOS, filenames...)
}

template.ParseFiles函数和ParseFiles方法都是调用底层函数parseFiles

复制代码
func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
    if err := t.checkCanParse(); err != nil {
        return nil, err
    }

    if len(filenames) == 0 {  //检查是否有模板文件名
        // Not really a problem, but be consistent.
        return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
    }
    for _, filename := range filenames {   //遍历模板文件名
        name, b, err := readFile(filename) //读取模板文件内容 
        if err != nil {
            return nil, err
        }
        s := string(b)        
        // First template becomes return value if not already defined,
        // and we use that one for subsequent New calls to associate
        // all the templates together. Also, if this file has the same name
        // as t, this file becomes the contents of t, so
        //  t, err := New(name).Funcs(xxx).ParseFiles(name)
        // works. Otherwise we create a new template associated with t.
        var tmpl *Template
        if t == nil {    //检查t是否为nil,为nil是template.ParseFiles函数调用
            t = New(name)  //若是nil则生成一个*Template实例,名字为第一个模板文件名
        }
        if name == t.Name() {//若t不为nil,为ParseFiles方法调用,检查所有的模板文件名
            tmpl = t     //是否有和t名字一样的,如果有则解析那个模板文件。
        } else {
            tmpl = t.New(name)
        }
        _, err = tmpl.Parse(s)
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}
复制代码

解析模板的几种方法:

①使用template.ParseFiles。模板名为第一模板文件名,使用t.Execute(w, data)使用模板只会使用执行第一个模板。

使用t.ExecuteTemplate(w, 模板文件名,data)可以指定使用哪个模板文件。

t, _ := template.ParseFiles("template2.html", "template1.html");
fmt.Println(t.Name())
//结果:template2.html

②先使用template.New(模板名)生成一个*Template实例,然后使用这个实例调用其ParseFiles()。New()中使用的模板名,要和传入ParseFiles()中的文件名中的一个对应,即解析了那个模板文件。

复制代码
//template1.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{.}}
</body>
</html>
复制代码
复制代码
//template2.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ol>{{.}}</ol>
</body>
</html>
复制代码
func main() {
    t := template.New("template2.html")
    fmt.Println(t.Name())    //输出t的模板名
    t, _ = t.ParseFiles("template2.html", "template1.html")
    t.Execute(os.Stdout, "hello")//输出内容
}

结果

复制代码
template2.html    //输出的模板名
<!DOCTYPE html>      //输出的内容     
<html lang="en">          
<head>                    
    <meta charset="UTF-8">
    <title>Title</title>  
</head>
<body>
    <ol>hello</ol>
</body>
</html>
复制代码

③解析字符串。使用*Template的方法Parse来解析字符串

func (t *Template) Parse(text string) (*Template, error)
复制代码
func articlesCreateHandler(w http.ResponseWriter, r *http.Request) {
    html := `
<!DOCTYPE html>     
<html lang="en">          
<head>                    
    <meta charset="UTF-8">
    <title>Title</title>  
</head>
<body>
    <ol>{{.}}</ol>
</body>
</html>
`
t := template.New("tmpl")
t, _ = t.Parse(html)
t.Execute(w, "hello world")
复制代码

模板动作

条件动作

 

posted on   博览天下with天涯海角  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示