gin中HTML渲染
package main import (
"github.com/gin-gonic/gin"
"net/http"
) func login(ctx *gin.Context) {
ctx.JSON(http.StatusOK, map[string]interface{}{
"username": "李四",
"password": 123465,
})
} func main() {
// HTML渲染,
router := gin.Default()
//router.LoadHTMLFiles("templates/index.html", "templates/login.html")
//router.LoadHTMLGlob("templates/*")
// 使用不同目录下名称相同的模板
router.LoadHTMLGlob("templates/**/*")
router.GET("/users/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "users/index.html", gin.H{
"title": "Users",
})
})
router.GET("/center/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "center/index.html", gin.H{
"title": "Center",
})
})
router.GET("/login", login)
router.Run()
}
目录结构

html代码
<!DOCTYPE html>
{{ define "center/index.html" }}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>{{ .title }}</h1>
</body>
</html>
{{ end }}
2. 自定义HTML模板渲染器
package main import (
"github.com/gin-gonic/gin"
"html/template"
"net/http"
) func main() {
router := gin.Default()
// 自定义html模板渲染器,要指定所有的html路径,不推荐
html := template.Must(template.ParseFiles(
"templates/login.html",
"templates/users/index.html",
"templates/center/index.html",
))
router.SetHTMLTemplate(html)
router.GET("/users/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "users/index.html", gin.H{
"title": "users/index.html",
})
})
router.Run()
}
3. 自定义分隔符、模板功
package main import (
"fmt"
"github.com/gin-gonic/gin"
"html/template"
"net/http"
"time"
) func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
} func main() {
router := gin.Default()
// 自定义分隔符
router.Delims("{[{", "}]}")
// 自定义模板功能
router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
// 加载模板文件路径
router.LoadHTMLGlob("templates/**/*")
router.GET("/users/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "users/index.html", map[string]interface{}{
"now": time.Date(2021, 10, 15, 0, 0, 0, 0, time.Local),
})
})
router.Run()
}
hmtl代码
<!DOCTYPE html>
{[{ define "users/index.html" }]}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>
{[{ .now | formatAsDate }]}
</h2>
</body>
</html>
{[{ end }]}
gin中HTML渲染的更多相关文章
- 【转载】OLE控件在Direct3D中的渲染方法
原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...
- Unity Shader入门精要学习笔记 - 第16章 Unity中的渲染优化技术
转自冯乐乐的 <Unity Shader 入门精要> 移动平台的特点 为了尽可能一处那些隐藏的表面,减少overdraw(即一个像素被绘制多次),PowerVR芯片(通常用于ios设备和某 ...
- Flask中的渲染变量
Flask中的渲染变量 一.渲染变量 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- [RN] React-Native中Array渲染的优化
React-Native中Array渲染的优化 例如用Push加进去的数据: constructor(props){ super(props); this.state = { b ...
- gin框架中的渲染
各种数据格式的响应 json.结构体.XML.YAML类似于java的properties.ProtoBuf 点击查看代码 // json响应 func someJson(context *gin.C ...
- gin中XML/JSON/YAML/ProtoBuf 渲染
package main import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/testdata/p ...
- 关于vue.js中列表渲染练习
html: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...
- 关于vue.js中条件渲染的练习
html: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...
- cocos2dx 3.x中的渲染机制
1.由2.x的渲染节点,变成添加渲染命令,可以避免重复渲染相同的节点,提高了渲染效率 2.单机游戏通常要求apk包在30M以内,没压缩1M会有1%的转换率(下载转换率),即收入会提高 3.2.x中首先 ...
随机推荐
- js判断是电脑(pc)访问还是手机(mobile)访问
<script> if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symb ...
- redis启动报错 var/run/redis_6379.pid exists, process is already running or crashed
redis启动显示 /var/run/redis_6379.pid exists, process is already running or crashed 出现这个执行 rm -rf /var/r ...
- JAVA获取本机的MAC地址
/** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...
- 设计owllook网络小说推荐系统
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 数据 推荐算法 协同过滤 基于流行度的推荐 基于用户标签相似度的推荐 评价指标 算 ...
- 【剑指Offer】翻转单词顺序列 解题报告(Python)
[剑指Offer]翻转单词顺序列 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...
- 知识增强的预训练语言模型系列之ERNIE:如何为预训练语言模型注入知识
NLP论文解读 |杨健 论文标题: ERNIE:Enhanced Language Representation with Informative Entities 收录会议:ACL 论文链接: ht ...
- 物联网大赛 - Android学习笔记(一) Android概念
一.Android 概念 Android是开放式的手机和电脑操作系统,是基于Linux系统做的上层开发. android可以做些什么? Android可以开发各种手机应用APP,也可以开发车载系统等, ...
- docker学习:docker---centos安装
查看目标镜像 docker search centos 拉取镜像 docker pull centos 查看镜像 docker images 启动镜像 docker run -itd --privil ...
- Kafka版本介绍Version2.4.0
1.说明 Kafka的版本从0.11.0.X到1.0.X, 再到2.0.X大版本, 其实没有经过几个版本, 只是版本号变化较大. 2.最新发布版本 截止本文章2020年2月22号发布时, Kafka ...
- CVE-2021-45232 Apache APISIX 从未授权访问到RCE
00x1漏洞环境 Apache APISIX Dashboard 2.7 - 2.10 版本受到影响 通过git拉取在docker搭建环境 git clone https://github.com/a ...