gin框架模板渲染

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

gin框架模板渲染

这个目录的路径要放正确(虽然我也不知道为什么突然就解决了)

== 错误模板 ==
在这里插入图片描述
== 正确版本==
在这里插入图片描述

package main

import (
	"net/http"

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

func main() {
	r := gin.Default()
	r.LoadHTMLGlob("templates/**/*") // ** 目录  *文件
	//r.LoadHTMLFiles("templates/posts/index.html", "templates/users/index.html")
	//路由
	r.GET("/posts/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
			"title": "郑艺好帅",
		})
	})
//路由2
	r.GET("users/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
			"title": "郑艺好可爱",
		})
	})

	r.Run(":8080")
}

== posts/index.tmpl==

{{define "posts/index.tmpl"}}
<!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>posts/index</title>
</head>
<body>
    {{.title}}
</body>
</html>
{{end}}

users/index/tmpl

{{define "users/index.tmpl"}}
<!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>users/index</title>
</head>
<body>
    {{.title}}
</body>
</html>
{{end}}

自定义模板函数

在这里插入图片描述

在index.tmpl中使用定义好的safe模板函数:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>修改模板引擎的标识符</title>
</head>
<body>
<div>{{ . | safe }}</div>
</body>
</html>

转义过的
在这里插入图片描述

未转义:
在这里插入图片描述

出现报错: undefined: template.HTML

template.HTML 是一个类型别名,用于表示安全的 HTML 文本。在 Go 语言中,使用 text/template 包时,可以使用 template.HTML 类型来确保 HTML 文本不会被转义,从而避免跨站脚本攻击(XSS)。

package main

import (
	"net/http"
	"text/template"

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

func main() {
	r := gin.Default()
	//gin框架中给模板添加自定义函数
	r.SetFuncMap(template.FuncMap{
		"safe": func(str string) template.HTML {
			return template.HTML(str)
		},
	})
	r.LoadHTMLGlob("templates/**/*") // ** 目录  *文件
	//r.LoadHTMLFiles("templates/posts/index.html", "templates/users/index.html")
	r.GET("/posts/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
			"title": "郑艺好帅",
		})
	})

	r.GET("users/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
			"title": "<a href = 'https://editor.csdn.net/md/?articleId=139142719'>郑艺的博客</a>",
		})
	})

	r.Run(":8080")
}

解决方案

import (
	"html/template"  //导入这个库
	"net/http"

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

静态文件处理

func main() {
	r := gin.Default()
	r.Static("/static", "./statics")//在解析静态模板之前,加上你的静态文件要去哪里找
	 //statics是存放静态文件的目录 当你在html文件中发现以xxx开头的就会指向statcis
	r.LoadHTMLGlob("templates/**/*") 
   // ...
	r.Run(":8080")
}

users/index,tmpl

<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">
    <link rel ="stylesheet" href = "/xxx/index.css">  //引用CS文件
    <title>users/index</title>
</head>
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值