All Projects → foolin → Gin Template

foolin / Gin Template

Licence: mit
golang template for gin framework!

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Gin Template

Goview
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.
Stars: ✭ 213 (+100.94%)
Mutual labels:  framework, gin, view, template
echo-template
golang template for echo framework!
Stars: ✭ 39 (-63.21%)
Mutual labels:  rice, view, render
go-gin-web-server
Deploy Go Gin on Render
Stars: ✭ 23 (-78.3%)
Mutual labels:  render, gin
Wpemerge
A modern, MVC-powered WordPress as a CMS workflow. 🚀
Stars: ✭ 348 (+228.3%)
Mutual labels:  framework, view
Primitive
⛏️ ‎ A front-end design toolkit for developing web apps.
Stars: ✭ 783 (+638.68%)
Mutual labels:  framework, template
ginadmin
基于Gin开发的后台管理系统,集成了、数据库操作、日志管理、权限分配管理、多模板页面、自动分页器、数据库迁移和填充、Docker集成部署等功能、静态资源打包
Stars: ✭ 149 (+40.57%)
Mutual labels:  render, gin
render react
Pre-render and mount React components from Ruby
Stars: ✭ 14 (-86.79%)
Mutual labels:  view, render
Noahv
An efficient front-end application framework based on vue.js
Stars: ✭ 593 (+459.43%)
Mutual labels:  framework, template
C cpp project framework
CMake build system( framework) with kconfig support for C/CPP projects
Stars: ✭ 26 (-75.47%)
Mutual labels:  framework, template
Androidlibs
🔥正在成为史上最全分类 Android 开源大全~~~~(长期更新 Star 一下吧)
Stars: ✭ 7,148 (+6643.4%)
Mutual labels:  framework, view
Think
ThinkPHP Framework ——十年匠心的高性能PHP框架
Stars: ✭ 7,681 (+7146.23%)
Mutual labels:  framework, template
blade
🏃 A library for using Laravel Blade templates in WordPlate.
Stars: ✭ 28 (-73.58%)
Mutual labels:  view, render
Zenbu
🏮 A Jinja2 + YAML based config templater.
Stars: ✭ 114 (+7.55%)
Mutual labels:  rice, template
Create Discord Bot
Create Discord bots using a simple widget-based framework.
Stars: ✭ 70 (-33.96%)
Mutual labels:  framework, template
Riot
Simple and elegant component-based UI library
Stars: ✭ 14,596 (+13669.81%)
Mutual labels:  framework, view
Gf
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
Stars: ✭ 6,501 (+6033.02%)
Mutual labels:  framework, template
Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+50816.04%)
Mutual labels:  framework, gin
Framework
ThinkPHP Framework
Stars: ✭ 2,399 (+2163.21%)
Mutual labels:  framework, template
Kube Render
Stars: ✭ 18 (-83.02%)
Mutual labels:  render, template
Miraigo Template
A template for MiraiGo
Stars: ✭ 47 (-55.66%)
Mutual labels:  framework, template

gin-template

GoDoc

Golang template for gin framework!

Deprecated!!!

Please consider trying to migrate to Goview

Goview

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.

Feature

  • Easy and simple to use for gin framework.
  • Use golang html/template syntax.
  • Support configure master layout file.
  • Support configure template file extension.
  • Support configure templates directory.
  • Support configure cache template.
  • Support include file.
  • Support dynamic reload template(disable cache mode).
  • Support multiple templates for fontend and backend.
  • Support go.rice add all resource files to a executable.

Docs

See https://www.godoc.org/github.com/foolin/gin-template

Install

go get github.com/foolin/gin-template

Usage

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/foolin/gin-template"
	"net/http"
)

func main() {
	router := gin.Default()

	//new template engine
	router.HTMLRender = gintemplate.Default()

	router.GET("/page", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "page.html", gin.H{"title": "Page file title!!"})
	})

	router.Run(":9090")
}

Configure

    TemplateConfig{
		Root:      "views", //template root path
		Extension: ".tpl", //file extension
		Master:    "layouts/master", //master layout file
		Partials:  []string{"partials/head"}, //partial files
		Funcs: template.FuncMap{
			"sub": func(a, b int) int {
				return a - b
			},
			// more funcs
		},
		DisableCache: false, //if disable cache, auto reload template file for debug.
	}

Render

Render with master

The ctx is instance of *gin.Context

//use name without extension `.html`
ctx.HTML(http.StatusOK, "index", gin.H{})

Render only file(not use master layout)

//use full name with extension `.html`
ctx.HTML(http.StatusOK, "page.html", gin.H{})

Include syntax

//template file
{{include "layouts/footer"}}

Examples

Basic example


package main

import (
	"github.com/gin-gonic/gin"
	"github.com/foolin/gin-template"
	"net/http"
)

func main() {
	router := gin.Default()

	//new template engine
	router.HTMLRender = gintemplate.Default()

	router.GET("/", func(ctx *gin.Context) {
		//render with master
		ctx.HTML(http.StatusOK, "index", gin.H{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})


	router.GET("/page", func(ctx *gin.Context) {
		//render only file, must full name with extension
		ctx.HTML(http.StatusOK, "page.html", gin.H{"title": "Page file title!!"})
	})

	router.Run(":9090")
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Basic example

Advance example


package main

import (
	"github.com/gin-gonic/gin"
	"github.com/foolin/gin-template"
	"net/http"
	"html/template"
	"time"
)

func main() {
	router := gin.Default()

	//new template engine
	router.HTMLRender = gintemplate.New(gintemplate.TemplateConfig{
		Root:      "views",
		Extension: ".tpl",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"sub": func(a, b int) int {
				return a - b
			},
			"copy": func() string{
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	router.GET("/", func(ctx *gin.Context) {
		//render with master
		ctx.HTML(http.StatusOK, "index", gin.H{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	router.GET("/page", func(ctx *gin.Context) {
		//render only file, must full name with extension
		ctx.HTML(http.StatusOK, "page.tpl", gin.H{"title": "Page file title!!"})
	})

	router.Run(":9090")
}

Project structure:

|-- app/views/
    |--- index.tpl          
    |--- page.tpl
    |-- layouts/
        |--- footer.tpl
        |--- head.tpl
        |--- master.tpl
    |-- partials/
        |--- ad.tpl
    

See in "examples/advance" folder

Advance example

Multiple example


package main

import (
	"html/template"
	"net/http"
	"time"

	"github.com/foolin/gin-template"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	//new template engine
	router.HTMLRender = gintemplate.New(gintemplate.TemplateConfig{
		Root:      "views/fontend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	router.GET("/", func(ctx *gin.Context) {
		// `HTML()` is a helper func to deal with multiple TemplateEngine's.
		// It detects the suitable TemplateEngine for each path automatically.
		gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{
			"title": "Fontend title!",
		})
	})

	//=========== Backend ===========//

	//new middleware
	mw := gintemplate.NewMiddleware(gintemplate.TemplateConfig{
		Root:      "views/backend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	// You should use helper func `Middleware()` to set the supplied
	// TemplateEngine and make `HTML()` work validly.
	backendGroup := router.Group("/admin", mw)

	backendGroup.GET("/", func(ctx *gin.Context) {
		// With the middleware, `HTML()` can detect the valid TemplateEngine.
		gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{
			"title": "Backend title!",
		})
	})

	router.Run(":9090")
}


Project structure:

|-- app/views/
    |-- fontend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        |-- partials/
     	   |--- ad.html
    |-- backend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        
See in "examples/multiple" folder

Multiple example

Block example


/*
 * Copyright 2018 Foolin.  All rights reserved.
 *
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 *
 */

package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
	"github.com/foolin/gin-template"
)

func main() {

	router := gin.Default()

	//new template engine
	router.HTMLRender = gintemplate.Default()

	router.GET("/", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index", gin.H{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})


	router.GET("/block", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "block", gin.H{"title": "Block file title!!"})
	})

	router.Run(":9090")
}



Project structure:

|-- app/views/
    |--- index.html          
    |--- block.html
    |-- layouts/
        |--- master.html
        
See in "examples/block" folder

Block example

go.rice example


/*
 * Copyright 2018 Foolin.  All rights reserved.
 *
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 *
 */

package main

import (
	"net/http"
	"github.com/GeertJohan/go.rice"
	"github.com/foolin/gin-template/supports/gorice"
	"github.com/gin-gonic/gin"
)

func main() {

	router := gin.Default()

	// servers other static files
	staticBox := rice.MustFindBox("static")
	router.StaticFS("/static", staticBox.HTTPBox())

	//new template engine
	router.HTMLRender = gorice.New(rice.MustFindBox("views"))

	// Routes
	router.GET("/", func(c *gin.Context) {
		//render with master
		c.HTML(http.StatusOK, "index", gin.H{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	router.GET("/page", func(c *gin.Context) {
		//render only file, must full name with extension
		c.HTML(http.StatusOK, "page.html", gin.H{"title": "Page file title!!"})
	})

	// Start server
	router.Run(":9090")
}



Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
|-- app/static/  
    |-- css/
        |--- bootstrap.css   	
    |-- img/
        |--- gopher.png

See in "examples/gorice" folder

gorice example

Supports

Relative Template

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].