Go Gin 模板继承

1 demo结构

在这里插入图片描述

2 main

package main

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

func main() {

	//创建一个默认的路由引擎
	httpServer := gin.Default()
	//加载静态文件
	httpServer.Static("/static", "./static")
	//渲染静态页面
	httpServer.LoadHTMLGlob("template/**/*")
	//index
	httpServer.GET("/index", handlerIndex)
	//product
	httpServer.GET("/product", handlerProduct)
	//启动HTTP服务器
	_ = httpServer.Run(":8080")
}

func handlerProduct(c *gin.Context) {
	c.HTML(200, "product.html", gin.H{
		"title":   "product",
		"content": "product content",
	})
}

func handlerIndex(c *gin.Context) {
	c.HTML(200, "index.html", gin.H{
		"title":   "index",
		"content": "index content",
	})
}

3 html

base_tmpl.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{.title}}</title>
    <link rel="stylesheet" href="static/css/common.css">
</head>
<header style="height: 200px;width: 100%;background: lightgreen;font-size: 20px;">
    COMMON HEADER
</header>
<body>

{{block "content" .}}
{{end}}

</body>
<footer style="height: 200px;width: 100%;background: lightcoral;font-size: 20px;">
    COMMON FOOTER
</footer>
</html>

index.html:

{{/*继承模板*/}}
{{template "base_tmpl.html" .}}

{{define "content"}}
    <h2>{{.content}}</h2>
{{end}}

product.html:

{{/*继承模板*/}}
{{template"base_tmpl.html" .}}


{{define"content"}}
    <h2>{{.content}}</h2>
{{end}}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

响彻天堂丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值