{"id":13794128,"url":"https://github.com/foolin/gin-template","last_synced_at":"2025-05-12T20:31:37.659Z","repository":{"id":57482238,"uuid":"117522088","full_name":"foolin/gin-template","owner":"foolin","description":"golang template for gin framework!","archived":true,"fork":false,"pushed_at":"2019-04-15T03:47:31.000Z","size":7347,"stargazers_count":122,"open_issues_count":2,"forks_count":23,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-09T11:03:27.263Z","etag":null,"topics":["framework","gin","go","golang","gonic","render","rice","template","view"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/foolin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-15T09:04:46.000Z","updated_at":"2024-09-01T13:51:49.000Z","dependencies_parsed_at":"2022-09-02T04:20:18.812Z","dependency_job_id":null,"html_url":"https://github.com/foolin/gin-template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foolin%2Fgin-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foolin%2Fgin-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foolin%2Fgin-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foolin%2Fgin-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foolin","download_url":"https://codeload.github.com/foolin/gin-template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253816766,"owners_count":21968878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","gin","go","golang","gonic","render","rice","template","view"],"created_at":"2024-08-03T23:00:36.255Z","updated_at":"2025-05-12T20:31:32.632Z","avatar_url":"https://github.com/foolin.png","language":"Go","readme":"# gin-template\n\n[![GoDoc](https://godoc.org/github.com/foolin/gin-template?status.png)](https://godoc.org/github.com/foolin/gin-template)\n\nGolang template for [gin framework](https://github.com/gin-gonic/gin)! \n\n**Deprecated!!!**\n\nPlease consider trying to migrate to [Goview](https://github.com/foolin/goview)\n\n# Goview\n[Goview](https://github.com/foolin/goview) is a lightweight, simple and easy template library based on golang html/template for building Go web application. Please consider trying to migrate to [Goview](https://github.com/foolin/goview). \n\n# Feature\n* Easy and simple to use for gin framework.\n* Use golang html/template syntax.\n* Support configure master layout file.\n* Support configure template file extension.\n* Support configure templates directory.\n* Support configure cache template.\n* Support include file.\n* Support dynamic reload template(disable cache mode).\n* Support multiple templates for fontend and backend.\n* Support [go.rice](https://github.com/foolin/gin-template/tree/master/supports/gorice) add all resource files to a executable.\n\n# Docs\nSee https://www.godoc.org/github.com/foolin/gin-template\n\n# Install\n```bash\ngo get github.com/foolin/gin-template\n```\n\n# Usage\n```go\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/foolin/gin-template\"\n\t\"net/http\"\n)\n\nfunc main() {\n\trouter := gin.Default()\n\n\t//new template engine\n\trouter.HTMLRender = gintemplate.Default()\n\n\trouter.GET(\"/page\", func(ctx *gin.Context) {\n\t\tctx.HTML(http.StatusOK, \"page.html\", gin.H{\"title\": \"Page file title!!\"})\n\t})\n\n\trouter.Run(\":9090\")\n}\n```\n\n# Configure\n\n```go\n    TemplateConfig{\n\t\tRoot:      \"views\", //template root path\n\t\tExtension: \".tpl\", //file extension\n\t\tMaster:    \"layouts/master\", //master layout file\n\t\tPartials:  []string{\"partials/head\"}, //partial files\n\t\tFuncs: template.FuncMap{\n\t\t\t\"sub\": func(a, b int) int {\n\t\t\t\treturn a - b\n\t\t\t},\n\t\t\t// more funcs\n\t\t},\n\t\tDisableCache: false, //if disable cache, auto reload template file for debug.\n\t}\n```\n\n\n# Render\n\n### Render with master\nThe `ctx` is instance of  `*gin.Context`\n```go\n//use name without extension `.html`\nctx.HTML(http.StatusOK, \"index\", gin.H{})\n```\n\n### Render only file(not use master layout)\n```go\n//use full name with extension `.html`\nctx.HTML(http.StatusOK, \"page.html\", gin.H{})\n```\n\n\n# Include syntax\n```go\n//template file\n{{include \"layouts/footer\"}}\n```\n\n# Examples\n\n### Basic example\n```go\n\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/foolin/gin-template\"\n\t\"net/http\"\n)\n\nfunc main() {\n\trouter := gin.Default()\n\n\t//new template engine\n\trouter.HTMLRender = gintemplate.Default()\n\n\trouter.GET(\"/\", func(ctx *gin.Context) {\n\t\t//render with master\n\t\tctx.HTML(http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Index title!\",\n\t\t\t\"add\": func(a int, b int) int {\n\t\t\t\treturn a + b\n\t\t\t},\n\t\t})\n\t})\n\n\n\trouter.GET(\"/page\", func(ctx *gin.Context) {\n\t\t//render only file, must full name with extension\n\t\tctx.HTML(http.StatusOK, \"page.html\", gin.H{\"title\": \"Page file title!!\"})\n\t})\n\n\trouter.Run(\":9090\")\n}\n\n```\n\nProject structure:\n```go\n|-- app/views/\n    |--- index.html          \n    |--- page.html\n    |-- layouts/\n        |--- footer.html\n        |--- master.html\n    \n\nSee in \"examples/basic\" folder\n```\n\n[Basic example](https://github.com/foolin/gin-template/tree/master/examples/basic)\n\n### Advance example\n```go\n\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/foolin/gin-template\"\n\t\"net/http\"\n\t\"html/template\"\n\t\"time\"\n)\n\nfunc main() {\n\trouter := gin.Default()\n\n\t//new template engine\n\trouter.HTMLRender = gintemplate.New(gintemplate.TemplateConfig{\n\t\tRoot:      \"views\",\n\t\tExtension: \".tpl\",\n\t\tMaster:    \"layouts/master\",\n\t\tPartials:  []string{\"partials/ad\"},\n\t\tFuncs: template.FuncMap{\n\t\t\t\"sub\": func(a, b int) int {\n\t\t\t\treturn a - b\n\t\t\t},\n\t\t\t\"copy\": func() string{\n\t\t\t\treturn time.Now().Format(\"2006\")\n\t\t\t},\n\t\t},\n\t\tDisableCache: true,\n\t})\n\n\trouter.GET(\"/\", func(ctx *gin.Context) {\n\t\t//render with master\n\t\tctx.HTML(http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Index title!\",\n\t\t\t\"add\": func(a int, b int) int {\n\t\t\t\treturn a + b\n\t\t\t},\n\t\t})\n\t})\n\n\trouter.GET(\"/page\", func(ctx *gin.Context) {\n\t\t//render only file, must full name with extension\n\t\tctx.HTML(http.StatusOK, \"page.tpl\", gin.H{\"title\": \"Page file title!!\"})\n\t})\n\n\trouter.Run(\":9090\")\n}\n\n```\n\nProject structure:\n```go\n|-- app/views/\n    |--- index.tpl          \n    |--- page.tpl\n    |-- layouts/\n        |--- footer.tpl\n        |--- head.tpl\n        |--- master.tpl\n    |-- partials/\n        |--- ad.tpl\n    \n\nSee in \"examples/advance\" folder\n```\n\n[Advance example](https://github.com/foolin/gin-template/tree/master/examples/advance)\n\n### Multiple example\n```go\n\npackage main\n\nimport (\n\t\"html/template\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"github.com/foolin/gin-template\"\n\t\"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n\trouter := gin.Default()\n\n\t//new template engine\n\trouter.HTMLRender = gintemplate.New(gintemplate.TemplateConfig{\n\t\tRoot:      \"views/fontend\",\n\t\tExtension: \".html\",\n\t\tMaster:    \"layouts/master\",\n\t\tPartials:  []string{\"partials/ad\"},\n\t\tFuncs: template.FuncMap{\n\t\t\t\"copy\": func() string {\n\t\t\t\treturn time.Now().Format(\"2006\")\n\t\t\t},\n\t\t},\n\t\tDisableCache: true,\n\t})\n\n\trouter.GET(\"/\", func(ctx *gin.Context) {\n\t\t// `HTML()` is a helper func to deal with multiple TemplateEngine's.\n\t\t// It detects the suitable TemplateEngine for each path automatically.\n\t\tgintemplate.HTML(ctx, http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Fontend title!\",\n\t\t})\n\t})\n\n\t//=========== Backend ===========//\n\n\t//new middleware\n\tmw := gintemplate.NewMiddleware(gintemplate.TemplateConfig{\n\t\tRoot:      \"views/backend\",\n\t\tExtension: \".html\",\n\t\tMaster:    \"layouts/master\",\n\t\tPartials:  []string{},\n\t\tFuncs: template.FuncMap{\n\t\t\t\"copy\": func() string {\n\t\t\t\treturn time.Now().Format(\"2006\")\n\t\t\t},\n\t\t},\n\t\tDisableCache: true,\n\t})\n\n\t// You should use helper func `Middleware()` to set the supplied\n\t// TemplateEngine and make `HTML()` work validly.\n\tbackendGroup := router.Group(\"/admin\", mw)\n\n\tbackendGroup.GET(\"/\", func(ctx *gin.Context) {\n\t\t// With the middleware, `HTML()` can detect the valid TemplateEngine.\n\t\tgintemplate.HTML(ctx, http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Backend title!\",\n\t\t})\n\t})\n\n\trouter.Run(\":9090\")\n}\n\n\n```\n\nProject structure:\n```go\n|-- app/views/\n    |-- fontend/\n        |--- index.html\n        |-- layouts/\n            |--- footer.html\n            |--- head.html\n            |--- master.html\n        |-- partials/\n     \t   |--- ad.html\n    |-- backend/\n        |--- index.html\n        |-- layouts/\n            |--- footer.html\n            |--- head.html\n            |--- master.html\n        \nSee in \"examples/multiple\" folder\n```\n\n[Multiple example](https://github.com/foolin/gin-template/tree/master/examples/multiple)\n\n\n### Block example\n```go\n\n/*\n * Copyright 2018 Foolin.  All rights reserved.\n *\n * Use of this source code is governed by a MIT style\n * license that can be found in the LICENSE file.\n *\n */\n\npackage main\n\nimport (\n\t\"net/http\"\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/foolin/gin-template\"\n)\n\nfunc main() {\n\n\trouter := gin.Default()\n\n\t//new template engine\n\trouter.HTMLRender = gintemplate.Default()\n\n\trouter.GET(\"/\", func(ctx *gin.Context) {\n\t\tctx.HTML(http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Index title!\",\n\t\t\t\"add\": func(a int, b int) int {\n\t\t\t\treturn a + b\n\t\t\t},\n\t\t})\n\t})\n\n\n\trouter.GET(\"/block\", func(ctx *gin.Context) {\n\t\tctx.HTML(http.StatusOK, \"block\", gin.H{\"title\": \"Block file title!!\"})\n\t})\n\n\trouter.Run(\":9090\")\n}\n\n\n\n```\n\nProject structure:\n```go\n|-- app/views/\n    |--- index.html          \n    |--- block.html\n    |-- layouts/\n        |--- master.html\n        \nSee in \"examples/block\" folder\n```\n\n[Block example](https://github.com/foolin/gin-template/tree/master/examples/block)\n\n### go.rice example\n```go\n\n/*\n * Copyright 2018 Foolin.  All rights reserved.\n *\n * Use of this source code is governed by a MIT style\n * license that can be found in the LICENSE file.\n *\n */\n\npackage main\n\nimport (\n\t\"net/http\"\n\t\"github.com/GeertJohan/go.rice\"\n\t\"github.com/foolin/gin-template/supports/gorice\"\n\t\"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n\n\trouter := gin.Default()\n\n\t// servers other static files\n\tstaticBox := rice.MustFindBox(\"static\")\n\trouter.StaticFS(\"/static\", staticBox.HTTPBox())\n\n\t//new template engine\n\trouter.HTMLRender = gorice.New(rice.MustFindBox(\"views\"))\n\n\t// Routes\n\trouter.GET(\"/\", func(c *gin.Context) {\n\t\t//render with master\n\t\tc.HTML(http.StatusOK, \"index\", gin.H{\n\t\t\t\"title\": \"Index title!\",\n\t\t\t\"add\": func(a int, b int) int {\n\t\t\t\treturn a + b\n\t\t\t},\n\t\t})\n\t})\n\n\trouter.GET(\"/page\", func(c *gin.Context) {\n\t\t//render only file, must full name with extension\n\t\tc.HTML(http.StatusOK, \"page.html\", gin.H{\"title\": \"Page file title!!\"})\n\t})\n\n\t// Start server\n\trouter.Run(\":9090\")\n}\n\n\n\n```\n\nProject structure:\n```go\n|-- app/views/\n    |--- index.html          \n    |--- page.html\n    |-- layouts/\n        |--- footer.html\n        |--- master.html\n|-- app/static/  \n    |-- css/\n        |--- bootstrap.css   \t\n    |-- img/\n        |--- gopher.png\n\nSee in \"examples/gorice\" folder\n```\n\n[gorice example](https://github.com/foolin/gin-template/tree/master/examples/gorice)\n\n# Supports\n\n- [go.rice](https://github.com/foolin/gin-template/tree/master/supports/gorice)\n\n# Relative Template\n\n- [Echo template](https://github.com/foolin/gin-template) The sample template for gin framework!\n","funding_links":[],"categories":["HTML Templates"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoolin%2Fgin-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoolin%2Fgin-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoolin%2Fgin-template/lists"}