Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
/ gint Public archive

GinT is an HTML template plugin for the great Gin framework written in Go

License

Notifications You must be signed in to change notification settings

janek-bieser/gint

Repository files navigation

GinT

Build Status Coverage Status Go Report Card GoDoc

GinT is an HTML template plug-in for the great Gin framework. It is somewhat opinionated about how templates are organized and rendered, but in exchange you won't have to write a lot of boilerplate code.

Getting Started

// example.go
import (
    "github.com/gin-gonic/gin"
    "github.com/janek-bieser/gint"
)

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

    // create the renderer and plug it into the gin framework
    r.HTMLRender = gint.NewHTMLRender()

    r.GET("/", func(c *gin.Context){
        c.HTML(http.StatusOK, "home", gin.H{"title": "Test"})
    })
    
    r.Run()
}
<!-- templates/layout.tmpl -->
<html>
    <head>
        <title>{{ .title }}</title>
    </head>
    <body>
        <h1>Example</h1>
        {{ template "content" . }}
    <body>
</html>
<!-- templates/home.tmpl -->
<h2>Home</h2>

The call to c.HTML(http.StatusOK, "home", gin.H{"title": "Test"}) will render the following HTML:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1>Example</h1>
        <h2>Home</h2>
    <body>
</html>

How it works

GinT expects you to organize all your HTML templates in a single folder. Your TemplateDir needs to contain a layout.* file which defines your application layout. The file extension of your templates is configurable (default is tmpl), so it could be called layout.html, layout.tpl.... Every time you call .HTML(status, templateName, data) on the gin.Context object, GinT will look inside the templates folder and render templateName inside your layout. In order for this to work, you have to render the content template inside your layout using {{ template "content" . }}.

TODO

  • cache templates in release mode
  • document how partials work
  • write some tests

About

GinT is an HTML template plugin for the great Gin framework written in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages