您的位置 首页 golang

golang从入门到精通,Gin Web开发,多模板继承详解

golang从入门到精通,Gin Web开发,多模板继承详解

在使用Golang Gin框架的时候,当不做前后端分离的时候,多模板渲染就显得尤为重要。

Gin默认的渲染模板是采用单模板模式,从这一点来说,很不方便,毕竟Gin项目通常是前后端分离的,而Gin主要做的是后端。

也不是说没有 多模板继承 的需求,网上找到的资料基本都是复制粘贴的,可操作性太差,没有进行详解。

下面我们一起学习下Gin的多模板继承,在这里我们使用到了一个block,这一点应该说是比较有实用价值的。

1、前言

 "github.com/gin-contrib/multitemplate"  

这个模块针对多模板使用,当然也能完成单模板的效果。

2、目录结构

 E:.
│  example.go
│
└─templates
    ├─admin
    │      index.html
    │      users.html
    │
    ├─base
    │      admin.html
    │      users.html
    │
    └─users
            admin.html
            index.html
  

3、模板文件

golang从入门到精通,Gin Web开发,多模板继承详解

admin文件夹下的源码

golang从入门到精通,Gin Web开发,多模板继承详解

users文件夹下的源码

golang从入门到精通,Gin Web开发,多模板继承详解

base文件夹下的源码

(1)admin文件夹存放的是前台展示的源码,继承base文件夹下的admin.html模板。

(2)users文件夹存放的是前台展示的源码,继承base文件夹下的users.html模板。

(3)base文件夹存放的是前台展示的源码,供admin文件夹和users文件夹继承使用。

在base文件夹的模板文件中,定义了一个{{block “body” .}} {{end}},这个位置就是填坑的位置。

4、主程序(由于源码较多,贴图可能看不清,因此直接写源码)

golang从入门到精通,Gin Web开发,多模板继承详解

主程序源码

 package main

import (
//引入multitemplate包,使得能够支持多模板功能
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"log"
)

func createMyRender() multitemplate.Renderer {
//func NewRenderer() Renderer
//NewRenderer allows create an agnostic multitemplate renderer depending on enabled gin mode
//multitemplate.NewRenderer()创建1个新的渲染器
r := multitemplate.NewRenderer()
//func (Renderer) AddFromFiles(name string, files ...string) *template.Template
//r.AddFromFiles()将从文件添加模板,有至少2个参数,name类型是string,是模板的名字,files是可变参数的string类型的参数
//这里需要说明下[]string和...string的区别:[]string 是字符串切片,...string用作参数,
//参数files需要写上需要解析的文件名,以最后一个文件名为主,其他的可作为被继承的模板使用,这个位置的顺序一定不能错。
r.AddFromFiles("users_index", "templates/base/users.html", "templates/users/index.html")
//这行解释一下,增加了1个名字为users_index的模板,渲染了"templates/base/users.html", "templates/users/index.html"两个文件
//在本示例中,其中"templates/base/users.html"作为了被继承的文件,"templates/users/index.html"作为前台文件
//users_index需要和c.HTML(200, "users_index", gin.H{})中的第二个参数relativePath匹配的
r.AddFromFiles("users_admin", "templates/base/users.html", "templates/users/admin.html")
r.AddFromFiles("admin_index", "templates/base/admin.html", "templates/admin/index.html")
r.AddFromFiles("admin_users", "templates/base/admin.html", "templates/admin/users.html")
//讲渲染器返回
return r
}

func main() {
app := gin.Default()
//将createMyRender()生成的渲染器加载到HTMLRender中,
//HTMLRender接口实现了HTMLProduction和HTMLDebug。
//HTMLProduction:包含模板引用。
//HTMLDebug包含模板和文件列表函数。
app.HTMLRender = createMyRender()
//func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes
//在接收GET POST PUT DELETE请求的时候,需要指定2个参数,
//第一个参数:请求的地址,URI
//第二个参数:处理的函数,这里写的匿名函数
app.GET("/users_index", func(c *gin.Context) {
//进行渲染HTML页面。有三个参数
//func (c *Context) HTML(code int, name string, obj interface{})
//第一个参数:状态码,可以是数字,也可以是状态码常量
//第二个参数:接收的模板名,注意:是模板名,并不是文件名,虽然通常文件名就是模板名,但是应该引起重视。
//第三个参数:传递给模板的参数,gin.H是map[string]interface{}的简写,实际上就是一个字典
c.HTML(200, "users_index", gin.H{
"title": "users_index",
})
})
app.GET("/users_admin", func(c *gin.Context) {
c.HTML(200, "users_admin", gin.H{
"title": "users_admin",
})
})

app.GET("/admin_index", func(c *gin.Context) {
c.HTML(200, "admin_index", gin.H{
"title": "admin_index",
})
})
app.GET("/admin_users", func(c *gin.Context) {
c.HTML(200, "admin_users", gin.H{
"title": "admin_users",
})
})

if err := app.Run("127.0.0.1:80"); err != nil {
log.Fatal(err)
}
}  

如果你需要,可以留言,发你打包源码,方便学习理解!

文章来源:智云一二三科技

文章标题:golang从入门到精通,Gin Web开发,多模板继承详解

文章地址:https://www.zhihuclub.com/87096.shtml

关于作者: 智云科技

热门文章

评论已关闭

38条评论

  1. Depression, diabetes, heart disease, high blood pressure, pulmonary arterial hypertension, high cholesterol, indigestion, obesity, alcohol intake, prostate cancer treatment, and spinal cord injury can all cause erectile dysfunction

  2. Extra Super Tadarise Generic Tadalafil 40 Mg and Dapoxetine 60 Mg is a propelled impotency pill which is utilized to treat both erectile brokenness and untimely discharge When neurologic injury occurs, penile hypoxia and fibrosis lead to the absence of spontaneous nocturnal erections, which decreases the release of nitric oxide 1,6

  3. Generic Viagra is the same medicine as Viagra , and Generic Cialis is the same medicine as Cialis , so the effect should be no different either better or worse

  4. Having pulmonary arterial hypertension PAH during pregnancy could harm may cause heart failure, stroke, or other medical problems in both mother and baby

  5. I was just amazed at the possibilities that it offers and wanted to pass it along to you lovely ladies.

  6. In vitro drug release is measured by using an apparatus masticator. 7 mg kg dose of tigecycline, once daily for four days, produced 77- 91 suppression in parasitaemia.

  7. The cross linking reaction of the mutually reacting polymers increases the viscosity of the solution to a critical point of gelation, at which time the solution is a cross linked, solid hydrogel

  8. The medication should be removed from the refrigerator by your provider 30 minutes prior to administration

  9. dbol 30mg 1 5 weeks test C 500mg 1 12 weeks Serious Use Alternative 1 carbamazepine will decrease the level or effect of dronedarone by affecting hepatic intestinal enzyme CYP3A4 metabolism

  10. for more than 12 weeks, has not been systematically evaluated in adequate and well controlled trials

  11. 2015 Jul; 99 Pt A 61 6 There are other proteins involved in double strand DNA repair and it is currently not known whether their expression would also be modulated by TH

  12. To explore the effect of diuretics on outcomes of AKI, studies often included patients using both UO and SCr criteria 2, 8

  13. There is increasing evidence from case control studies, though less strongly from cohort studies, that use of aspirin or NSAIDs over about 20 years is associated with a reduced risk of oesophageal cancer Figure 1, with less conclusive evidence of a lower risk of stomach cancer Figure 2 6

  14. The level of estrogen slowly rises during this period as the uterus lining thickens and starts its preparation to receive the egg if ovulation occurs Spinal bone mineral density BMD is directly correlated with the strength of the back extensors; therefore, maintaining and strengthening the back extensors should be emphasized

  15. With its unique role in directly stimulating sensory neurons related to itch and contributing to inflammation and barrier dysfunction, IL 31 is the bridge between the immune and nervous systems while directly acting on structural cells in the skin 4m new broadband and phone subscribers

  16. About Alliance Foundation Trials Alliance Foundation Trials, LLC AFT is a research organization that develops and conducts cancer clinical trials, working closely with the Alliance for Clinical Trials in Oncology scientific investigators and institutional member network, research collaborators, and non NCI funding sources 2012; 38 1 23 8

  17. Studies investigating the association between sleep disordered breathing and stroke found that 60 to 70 percent of individuals who have suffered a stroke exhibit sleep disordered breathing with an apnea hypopnea index of 10 or greater Dyken et al Poles became more and more aware of the free market economy; they had lots of ideas and a great desire for personal development, but our country did not offer such opportunities

  18. This may cause a straining of the muscles in the shoulder and neck and thereby result in a more frequent experience of pain Pediat Nephrol Berlin, Germany 18 8 772 777

  19. The use of an untreated subfertile population as the referent category for a variety of treatment exposures is a strength of this study and may present a treatment effect magnitude that at least partially controls for misclassification of fertility related diagnosis and undiagnosed subfertility pathology

  20. Some of the mice received doxycycline, 2 mg kg in the drinking water starting 72 h prior to the administration of LPS This study was approved by the Ethics Committee of INT Fondazione Pascale

  21. Local small businesses generally less affected by price increases S allylcysteine ameliorates doxorubicin toxicity in the heart and liver in mice

网站地图