Skip to content

Custom render for gin-gonic by "text/template" instead "html/template"

License

Notifications You must be signed in to change notification settings

night-codes/go-gin-ttpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

go-gin-ttpl

Custom render for gin-gonic by "text/template" instead "html/template"

Why Do It?

Sometimes we need to render templates without HTML. Also "html/template" has the problem with parser.

How To Use

package main

import (
    "gopkg.in/gin-gonic/gin.v1"
    "gopkg.in/night-codes/go-gin-ttpl.v1"
)

func main() {
    r := gin.New()
    ttpl.Use(r, "templates/*")
    r.GET("/", func(c *gin.Context) {
        c.HTML(200, "index.html", gin.H{"title": "top"})
    })
    r.Run(":41000")
}

When you want to add your own template functions you can pass a third parameter - FuncMap

import (
    // ...
    "text/template"
)

func main() {
    // ...
    funcMap := template.FuncMap{
        "customFunc": customFunc, // A custom template function
    }
    ttpl.Use(r, "templates/*", funcMap) // <--
    // ...
}

License

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004

Copyright (C) 2015 Oleksiy Chechel alex.mirrr@gmail.com

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  1. You just DO WHAT THE FUCK YOU WANT TO.

About

Custom render for gin-gonic by "text/template" instead "html/template"

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages