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渲染的更多相关文章

  1. 【转载】OLE控件在Direct3D中的渲染方法

    原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...

  2. Unity Shader入门精要学习笔记 - 第16章 Unity中的渲染优化技术

    转自冯乐乐的 <Unity Shader 入门精要> 移动平台的特点 为了尽可能一处那些隐藏的表面,减少overdraw(即一个像素被绘制多次),PowerVR芯片(通常用于ios设备和某 ...

  3. Flask中的渲染变量

    Flask中的渲染变量 一.渲染变量 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. [RN] React-Native中Array渲染的优化

    React-Native中Array渲染的优化 例如用Push加进去的数据: constructor(props){    super(props);    this.state = {      b ...

  5. gin框架中的渲染

    各种数据格式的响应 json.结构体.XML.YAML类似于java的properties.ProtoBuf 点击查看代码 // json响应 func someJson(context *gin.C ...

  6. gin中XML/JSON/YAML/ProtoBuf 渲染

    package main import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/testdata/p ...

  7. 关于vue.js中列表渲染练习

    html: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...

  8. 关于vue.js中条件渲染的练习

    html: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...

  9. cocos2dx 3.x中的渲染机制

    1.由2.x的渲染节点,变成添加渲染命令,可以避免重复渲染相同的节点,提高了渲染效率 2.单机游戏通常要求apk包在30M以内,没压缩1M会有1%的转换率(下载转换率),即收入会提高 3.2.x中首先 ...

随机推荐

  1. c++之面试题(1)

    题目 有十瓶药,每瓶里都装有100片药,其中有八瓶里的药每片重10克,另有两瓶里的药每片重9克.用一个蛮精确的小秤,只称一次,如何找出份量较轻的那两个药瓶? 解法 1.分别给10个药瓶按照斐波那契数列 ...

  2. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  3. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  4. 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  5. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  6. (原创)WinForm中莫名其妙的小BUG——RichTextBox自动选择字词问题

    一.前言 使用WinForm很久了,多多少少会遇到一些小BUG. 这些小BUG影响并不严重,而且稍微设置一下就能正常使用,而且微软一直也没有修复这些小BUG. 写本系列文章,是为了记录一下这些无伤大雅 ...

  7. IntelliJ IDEA打war包

    1.按ctrl+alt+shift+s键打开Project Structure,点击+号图标,选择"Artifacts->Web Application Archive" 2 ...

  8. 云南农职《JavaScript交互式网页设计》 综合机试试卷③——实现二级分类菜单

    一.语言和环境 实现语言:HTML,CSS,JavaScript,JQuery. 开发环境:HBuilder. 二.题目(100分): 使用Jquery和JavaScript实现二级分类菜单管理 点击 ...

  9. 引用element-ui的Drawer抽屉组件报错问题

    前提:vue项目采取按需引入的方式引入element,并且使用其他组件都正常,没有发生异常 问题表现: 在vue项目中引用了Drawer 抽屉组件,结果报错 意思就是组件未注册,当时我的表情: 没办法 ...

  10. C# 高性能对象复制

    需求背景:对象复制性能优化:同时,在对象复制时,应跳过引用类型的null值复制,值类型支持值类型向可空类型的复制 -------------- 1 using Common; 2 using Syst ...