gint

package module
v0.0.0-...-c061e6d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 7 Imported by: 0

README

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

Documentation

Overview

Package gint is an HTML Template plugin for the Gin (https://godoc.org/github.com/gin-gonic/gin) framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTMLRender

type HTMLRender struct {
	TemplateDir string
	TemplateExt string
	LayoutFile  string
}

HTMLRender is an implementation of the render.HTMLRender interface defined by the gin framework. It holds information like the location or file extension of your templates.

func NewHTMLRender

func NewHTMLRender() *HTMLRender

NewHTMLRender constructs a default HTMLRender object.

func (*HTMLRender) Instance

func (r *HTMLRender) Instance(name string, data interface{}) render.Render

Instance is the implementation of the render.HTMLRender interface of the gin framework.

Directories

Path Synopsis
Package main is the example package.
Package main is the example package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL