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中首先 ...
随机推荐
- Mybatis一对一、一对多级联查询使用
在A对象的xml配置文件中 一对一<association property="shop" column="shop_id" select="c ...
- ubuntu无法找到ifconfig(command not found: ifconfig)
演示环境 $ uname -a Linux xxxxx-xxxxx-xxxx5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x8 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 1289 - LCM from 1 to n
http://blog.csdn.net/acdreamers/article/details/18507767 这个是位图的链接,这篇写的挺好. 模板: 1 #include<math.h&g ...
- 洛谷 P2397:yyy loves Maths VI (mode)(摩尔投票算法)
题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居然也不会,所以只好找你 题目描述 [h1]udp2:第一题因为语 ...
- <学习opencv> opencv 概述及初探
目录 Opencv3 当前模块 OpenCV 贡献库(opencv_contrib) OpenCV 头文件 旧式C风格头文件 新式C++风格头文件 例程 DEMO1 - 显示图片 DEMO2 - 视频 ...
- Capstone CS5268DEMOBOARD原理图|TYPEC转HDMI+VGA+PD3.0+USB3.0扩展坞方案
Capstone CS5268DEMOBOARD原理图|TYPEC转HDMI+VGA+PD3.0+USB3.0四合一设计参考 CS5268 是typec转HDMI+VGA+pd3.0+U3四合一拓展坞 ...
- tcache BUUCTF gyctf_2020_signin
Ubuntu18.04的题 用到了两个特性: 一个是 calloc 的特点:不会分配 tcache chunk 中的 chunk 另一个是 tcache 的特点:在分配 fastbin 中的 chun ...
- 基于Spring MVC + Spring + MyBatis的【银行卡系统】
资源下载:https://download.csdn.net/download/weixin_44893902/45604256 练习点设计: 删除.新增 一.语言和环境 实现语言:JAVA语言. 环 ...
- Eclipse远程调试Java代码的三种方法
Eclipse远程调试Java代码的三种方法, 第1种方法是用来调试已经启动的Java程序,Eclipse可以随时连接到远程Java程序进行调试, 第2种方法可以调试Java程序启动过程,但是Ecli ...