golang学习之gin--第⑤篇:模板渲染、嵌套和继承

目录

一、模板渲染

1、一级模板渲染

目录结构
在这里插入图片描述

./main.go 文件:

package main

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

func main() {
	router := gin.Default()
	// 指定模板路径
	router.LoadHTMLGlob("template/*")

	router.GET("/index", func(c *gin.Context) {
		//  HTML(code int, name string, obj interface{}): 状态码, 模板文件, 模板参数
		c.HTML(http.StatusOK, "index.html", gin.H{"title": "我是测试"})
	})

	router.Run()
}

./tempalte/index.html 文件:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    {{.title}}
</body>
</html>

运行结果:
在这里插入图片描述
在这里插入图片描述

2、多级模板渲染

目录结构
在这里插入图片描述
./main.go 文件:

package main

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

func main() {
	router := gin.Default()
	// 指定模板路径
	router.LoadHTMLGlob("template/**/*")

	router.GET("/user", func(c *gin.Context) {
		//  HTML(code int, name string, obj interface{}): 状态码, 模板文件, 模板参数
		c.HTML(http.StatusOK, "user.html", gin.H{"title": "我是多级模板测试"})
	})

	router.Run()
}

tempalte/user/user.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{.title}}
</body>
</html>

运行结果:
在这里插入图片描述
在这里插入图片描述

二、模板嵌套

1、模板嵌套

目录结构
在这里插入图片描述

main.go 文件:

package main

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

func main() {
	router := gin.Default()
	// 指定模板路径
	router.LoadHTMLGlob("template/**/*")

	router.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index/user.html", gin.H{"title": "我是模板继承测试"})
	})

	router.Run()
}

tempalte/public/header.html 文件:

{{define "public/header"}}
<!DOCTYPE html>
<html lang="en">
<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>header</title>
</head>
<body>
<p>我是头部html</p>
{{end}}

tempalte/public/footer.html 文件:

{{define "public/footer"}}
<p>我是尾部html</p>
</body>
</html>
{{ end }}

tempalte/user/user.html 文件:

{{ define "user/index.html" }}
{{template "public/header" .}}
title:{{.title}}
{{template "public/footer" .}}
{{ end }}

运行结果:
在这里插入图片描述
在这里插入图片描述

三、模板继承

1、模板继承

目录结构
在这里插入图片描述

main.go 文件:

package main

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

func main() {
	router := gin.Default()
	// 布局页面
	template.ParseGlob("layout.html")
	// 配置模板
	router.LoadHTMLGlob("template/**/*")

	router.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "user.html", gin.H{"title": "我是模板继承测试"})
	})

	router.Run()
}


tempalte/public/header.html 文件:

{{define "public/header"}}
<!DOCTYPE html>
<html lang="en">
<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>header</title>
</head>
<body>
<p>我是头部html</p>
{{end}}

tempalte/public/footer.html 文件:

{{define "public/footer"}}
<p>我是尾部html</p>
</body>
</html>
{{ end }}

tempalte/public/layout.html 文件:

{{define "public/layout"}}
    {{template "public/header" .}}
    <!--内容部分-->
    <div class="container">
        {{block "content" . }}{{end}}
    </div>
    {{template "public/footer" .}}
{{ end }}

tempalte/user/user.html 文件:

{{template "public/layout" .}}

{{ define "content" }}
    <p>
        内容-title:{{.title}}
    </p>
{{end}}

运行结果:
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值