美文网首页
Beego 框架学习笔记 05 | 视图、模板引擎

Beego 框架学习笔记 05 | 视图、模板引擎

作者: Wonz | 来源:发表于2021-01-20 21:38 被阅读0次

    一、模板引擎

    1. 模板中绑定基本数据、字符串、数值、布尔值

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
    
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
    
    </body>
    </html>
    

    显示:

    image

    2. 模板中绑定结构体数据

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
        
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
    </body>
    </html>
    

    显示:

    image

    3. 模板中自定义变量

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
    </body>
    </html>
    

    显示:

    image

    4. 模板中循环遍历 range 模板中循环切片

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
    
    </body>
    </html>
    

    显示:

    image

    5. 模板中循环遍历 range 模板中循环 Map

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        // 5. 模板中循环遍历 range 模板中循环 Map
        userinfo := make(map[string]interface{})
        userinfo["username"] = "张三"
        userinfo["age"] = 20
        userinfo["sex"] = "男"
        c.Data["userinfo"] = userinfo
    
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
    
    </body>
    </html>
    

    显示:

    image

    6. 模板中循环遍历结构体类型的切片

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        // 5. 模板中循环遍历 range 模板中循环 Map
        userinfo := make(map[string]interface{})
        userinfo["username"] = "张三"
        userinfo["age"] = 20
        userinfo["sex"] = "男"
        c.Data["userinfo"] = userinfo
    
        // 6. 模板中循环遍历结构体类型的切片
        c.Data["articleList"] = []Article{
            {
                Title: "新闻1",
                Content: "新闻内容1",
            },
            {
                Title: "新闻2",
                Content: "新闻内容2",
            },
            {
                Title: "新闻3",
                Content: "新闻内容3",
            },
        }
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
    </body>
    </html>
    

    显示:

    image

    7. 结构体类型的切片的另一种定义方式

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        // 5. 模板中循环遍历 range 模板中循环 Map
        userinfo := make(map[string]interface{})
        userinfo["username"] = "张三"
        userinfo["age"] = 20
        userinfo["sex"] = "男"
        c.Data["userinfo"] = userinfo
    
        // 6. 模板中循环遍历结构体类型的切片
        c.Data["articleList"] = []Article{
            {
                Title: "新闻1",
                Content: "新闻内容1",
            },
            {
                Title: "新闻2",
                Content: "新闻内容2",
            },
            {
                Title: "新闻3",
                Content: "新闻内容3",
            },
        }
    
        // 7. 结构体类型的切片的另一种定义方式
    
        /*
            匿名结构体,它就是一个类型
            struct {
                Title string
            }
         */
        c.Data["cmsList"] = []struct{
            Title string
        }{
            {
                Title: "新闻1",
            },
            {
                Title: "新闻2",
            },
            {
                Title: "新闻3",
            },
        }
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
      <h2>7. 结构体类型的切片的另一种定义方式</h2>
      <ul>
        {{range $key,$val := .cmsList}}
          <li>{{$key}}---{{$val.Title}}</li>
        {{end}}
      </ul>
    
    </body>
    </html>
    

    显示:

    image

    8. 模板中的条件

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        // 5. 模板中循环遍历 range 模板中循环 Map
        userinfo := make(map[string]interface{})
        userinfo["username"] = "张三"
        userinfo["age"] = 20
        userinfo["sex"] = "男"
        c.Data["userinfo"] = userinfo
    
        // 6. 模板中循环遍历结构体类型的切片
        c.Data["articleList"] = []Article{
            {
                Title: "新闻1",
                Content: "新闻内容1",
            },
            {
                Title: "新闻2",
                Content: "新闻内容2",
            },
            {
                Title: "新闻3",
                Content: "新闻内容3",
            },
        }
    
        // 7. 结构体类型的切片的另一种定义方式
    
        /*
            匿名结构体,它就是一个类型
            struct {
                Title string
            }
         */
        c.Data["cmsList"] = []struct{
            Title string
        }{
            {
                Title: "新闻1",
            },
            {
                Title: "新闻2",
            },
            {
                Title: "新闻3",
            },
        }
    
        // 8. 模板中的条件判断
        c.Data["isLogin"] = true
        c.Data["isHome"] = true
        c.Data["isAbout"] = true
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
      <h2>7. 结构体类型的切片的另一种定义方式</h2>
      <ul>
        {{range $key,$val := .cmsList}}
          <li>{{$key}}---{{$val.Title}}</li>
        {{end}}
      </ul>
    
      <h2>8. 模板中的条件</h2>
      {{if .isLogin}}
        <p>isLogin等于true</p>
      {{end}}
    
      {{if .isHome}}
        <p>isHome等于true</p>
        {{else}}
        <p>isHome等于false</p>
        {{end}}
    
    
    </body>
    </html>
    

    显示:

    image

    9. if 语句条件判断 eq/ne/lt/le/gt/ge

    default.go:

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    type Article struct {
        Title string
        Content string
    }
    
    func (c *MainController) Get() {
        // 1. 模板中绑定基本数据、字符串、数值、布尔值
        c.Data["website"] = "beego 教程"
        c.Data["title"] = "你好beego"
        c.Data["num"] = 12
        c.Data["flag"] = true
    
        // 2. 模板中绑定结构体数据
        article := Article{
            Title: "Golang教程",
            Content: "beego实战项目",
        }
        c.Data["article"] = article
    
        // 4. 模板中循环遍历 range 模板中循环切片
        c.Data["sliceList"] = []string{"php", "golang", "python"}
    
        // 5. 模板中循环遍历 range 模板中循环 Map
        userinfo := make(map[string]interface{})
        userinfo["username"] = "张三"
        userinfo["age"] = 20
        userinfo["sex"] = "男"
        c.Data["userinfo"] = userinfo
    
        // 6. 模板中循环遍历结构体类型的切片
        c.Data["articleList"] = []Article{
            {
                Title: "新闻1",
                Content: "新闻内容1",
            },
            {
                Title: "新闻2",
                Content: "新闻内容2",
            },
            {
                Title: "新闻3",
                Content: "新闻内容3",
            },
        }
    
        // 7. 结构体类型的切片的另一种定义方式
    
        /*
            匿名结构体,它就是一个类型
            struct {
                Title string
            }
         */
        c.Data["cmsList"] = []struct{
            Title string
        }{
            {
                Title: "新闻1",
            },
            {
                Title: "新闻2",
            },
            {
                Title: "新闻3",
            },
        }
    
        // 8. 模板中的条件判断
        c.Data["isLogin"] = true
        c.Data["isHome"] = true
        c.Data["isAbout"] = true
    
        // 9. if 语句条件判断 eq/ne/lt/le/gt/ge
        c.Data["n1"] = 12
        c.Data["n2"] = 6
    
        c.TplName = "index.html"
    }
    

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
      <h2>7. 结构体类型的切片的另一种定义方式</h2>
      <ul>
        {{range $key,$val := .cmsList}}
          <li>{{$key}}---{{$val.Title}}</li>
        {{end}}
      </ul>
    
      <h2>8. 模板中的条件</h2>
      {{if .isLogin}}
        <p>isLogin等于true</p>
      {{end}}
    
      {{if .isHome}}
        <p>isHome等于true</p>
        {{else}}
        <p>isHome等于false</p>
        {{end}}
    
      <h2>9. if 语句条件判断 eq/ne/lt/le/gt/ge</h2>
      {{if gt .n1 .n2}}
      <p>n1大于n2</p>
      {{end}}
    
      {{if eq .n1 .n2}}
        <p>n1等于n2</p>
      {{else}}
        <p>n1不等于n2</p>
      {{end}}
    
    </body>
    </html>
    

    显示:

    image

    10. define 自定义模板

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
      <h2>7. 结构体类型的切片的另一种定义方式</h2>
      <ul>
        {{range $key,$val := .cmsList}}
          <li>{{$key}}---{{$val.Title}}</li>
        {{end}}
      </ul>
    
      <h2>8. 模板中的条件</h2>
      {{if .isLogin}}
        <p>isLogin等于true</p>
      {{end}}
    
      {{if .isHome}}
        <p>isHome等于true</p>
        {{else}}
        <p>isHome等于false</p>
        {{end}}
    
      <h2>9. if 语句条件判断 eq/ne/lt/le/gt/ge</h2>
      {{if gt .n1 .n2}}
      <p>n1大于n2</p>
      {{end}}
    
      {{if eq .n1 .n2}}
        <p>n1等于n2</p>
      {{else}}
        <p>n1不等于n2</p>
      {{end}}
    
      <h2>10. define 自定义模板</h2>
      {{define "a"}}
        <h4>这是一个自定义的代码块</h4>
          <p>111</p>
      {{end}}
    
      <div>
        {{template "a" .}}
      </div>
    
    </body>
    </html>
    

    显示:

    image

    11. 外部自定义模板

    index.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>{{.Website}}</title>
    </head>
    <body>
    {{template "/public/header.html"}}
      <h2>1. 模板中绑定基本数据、字符串、数值、布尔值</h2>
      <p>{{.title}}</p>
      <p>{{.num}}</p>
      <p>{{.flag}}</p>
      <br>
    
      <h2>2. 模板中绑定结构体数据</h2>
      <p>{{.article.Title}}</p>
      <p>{{.article.Content}}</p>
      <br>
    
      <h2>3. 模板中自定义变量</h2>
      {{$title := .title}}
      {{$title}}
    
      <h2>4. 模板中循环遍历 range 模板中循环切片</h2>
      <ul>
        {{range $key,$val := .sliceList}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>5. 模板中循环遍历 range 模板中循环 Map</h2>
      <ul>
        {{range $key,$val := .userinfo}}
          <li>{{$key}}---{{$val}}</li>
        {{end}}
      </ul>
    
      <h2>6. 模板中循环遍历结构体类型的切片</h2>
      <ul>
        {{range $key,$val := .articleList}}
          <li>{{$key}}---{{$val.Title}}---{{$val.Content}}</li>
        {{end}}
      </ul>
    
      <h2>7. 结构体类型的切片的另一种定义方式</h2>
      <ul>
        {{range $key,$val := .cmsList}}
          <li>{{$key}}---{{$val.Title}}</li>
        {{end}}
      </ul>
    
      <h2>8. 模板中的条件</h2>
      {{if .isLogin}}
        <p>isLogin等于true</p>
      {{end}}
    
      {{if .isHome}}
        <p>isHome等于true</p>
        {{else}}
        <p>isHome等于false</p>
        {{end}}
    
      <h2>9. if 语句条件判断 eq/ne/lt/le/gt/ge</h2>
      {{if gt .n1 .n2}}
      <p>n1大于n2</p>
      {{end}}
    
      {{if eq .n1 .n2}}
        <p>n1等于n2</p>
      {{else}}
        <p>n1不等于n2</p>
      {{end}}
    
      <h2>10. define 自定义模板</h2>
      {{define "a"}}
        <h4>这是一个自定义的代码块</h4>
          <p>111</p>
      {{end}}
    
      <div>
        {{template "a" .}}
      </div>
    
    
      <h2>11. 外部自定义模板</h2>
    {{template "/public/footer.html"}}
    </body>
    </html>
    

    在 views 文件夹下新建 public 文件夹,然后新建 header.html 和 footer.html

    header.html:

    <h1>这是一个头部</h1>
    

    footer.html:

    <style>
        footer{
            width: 100%;
            height: 60px;
            background: #000;
            text-align: center;
            color: #fff;
        }
    </style>
    
    <footer>
        <h1>这是一个底部</h1>
    </footer>
    

    显示:

    image

    二、内置模板函数

    在 controllers 文件夹下新建一个 article.go 文件:

    package controllers
    
    import (
        "github.com/astaxie/beego"
        "time"
    )
    
    type ArticleController struct {
        beego.Controller
    }
    
    func (c *ArticleController) Get() {
        c.Data["title"] = "你好beego"
        now := time.Now()
        c.Data["now"] = now  // 把日期渲染到模板上
        c.TplName = "article.html"
    }
    

    在 router.go 中添加路由:

    package routers
    
    import (
        "beegodemo03/controllers"
        "github.com/astaxie/beego"
    )
    
    func init() {
        beego.Router("/", &controllers.MainController{})
        beego.Router("/article", &controllers.ArticleController{})
    }
    

    views 文件夹下新建一个 article.html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/html">
    <head>
      <meta charset="UTF-8">
      <title>文章</title>
    </head>
    <body>
      {{template "/public/header.html"}}
      <p>{{.now}}</p>
      <p>{{date .now "Y-m-d H:i:s"}}</p>
      {{template "/public/footer.html"}}
    </body>
    </html>
    

    显示:

    image

    三、参考教程

    Golang 教程 P56

    相关文章

      网友评论

          本文标题:Beego 框架学习笔记 05 | 视图、模板引擎

          本文链接:https://www.haomeiwen.com/subject/kwaogktx.html