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. 使用docker创建含有FFmpeg的自定义镜像

    Dockerfile文件 FROM openjdk:8-jre-alpine MAINTAINER "yvioo" RUN echo "http://mirrors.al ...

  2. JS判断是否为“YYYYMMDD”式的日期

    function isDate8(sDate) { if (!/^[0-9]{8}$/.test(sDate)) { return false; } var year, month, day; yea ...

  3. Linux(centos)下修改mysql的sql_mode模式

    进入MySQL的配置文件 默认是/etc/my.cnf vim my.cnf 在最后一行加入 sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITU ...

  4. 【LeetCode】429. N-ary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  5. css--深入理解z-index引发的层叠上下文、层叠等级和层叠顺序

    前言 在编写css样式代码的时候,我们经常会遇到z-index属性的使用,我们可能只了解z-index能够提高元素的层级,并不知道具体是怎么实现的.本文就来总结一个由z-index 引发的层叠上下文和 ...

  6. 【jvm】06-new一个对象到底占了多少内存?

    [jvm]06-new一个对象到底占了多少内存? 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请顺 ...

  7. AES对称加密算法实现:Java,C#,Golang,Python

    高级加密标准(Advanced Encryption Standard,简写AES),是一种用来替代DES的对称加密算法,相比DES,AES安全性更高,加密速度更快,因此被广泛使用. 理论上看,AES ...

  8. 关于一类容斥原理设计 dp 状态的探讨

    写在前面 为什么要写?因为自己学不明白希望日后能掌握. 大体思路大概是 设计一个容斥的方案,并使其贡献可以便于计算. 得出 dp 状态,然后优化以得出答案. 下列所有类似 \([l,r]\) 这样的都 ...

  9. Thrift框架-安装

    1.前言 今天接触了使用 PRC[远程过程调用协议]的Thrift 框架  ,留下随笔心得,这是安装篇 2.下载 去apache官网下载Thrift脚本编译程序,window则下载一个exe文件,然后 ...

  10. vue组件中的.sync修饰符使用

    在vue的组件通信props中,一般情况下,数据都是单向的,子组件不会更改父组件的值,那么vue提供.sync作为双向传递的关键字,实现了父组件的变动会传递给子组件,而子组件的carts改变时,通过事 ...