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

An HTML Renderer for Gin Gonic with support for layout, partials and datasources

License

Notifications You must be signed in to change notification settings

benweidig/giraffe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Giraffe Build Status

A Go template renderer I've created for usage with Gin Gonic, supporting layouts, partials and different datasources like packr. It should be usable without Gin, at least I'm going to try in another project.

Get it

go get -u github.com/benweidig/giraffe

Use it

You can use Giraffe for simple templating, or with Gin Gonic. The package gin mimics the package giraffe, it even uses the same names, but the Giraffe is gin.HTMLRender compatible.

If you're using the gin-part you might want to rename the import to giraffe, I assume it for the examples in the README.

The simple way for Gin

You can simply use giraffe.Default() to get a usable Giraffe for Gin Gonic:

import (
    giraffe "github.com/benweidig/giraffe/gin"
)

func yourSetupMethodMaybe() {
    r := gin.New()

    r.HTMLRender = giraffe.Default()

    <or>

    r.HTMLRender = giraffe.Debug()
}

Both versions have some sensible defaults:

Setting Default() Debug() Description
Datasource fs.Default() fs.Default() A filesystem-based datasourse, see below
Layout layout layout Filename/path of layout file, without extension
Funcs [] [] User-supplied template functions
DisableCache false true Caching

The harder way

You can also create a Giraffe with your own config:

import (
    giraffe "github.com/benweidig/giraffe/gin"
    // You should rename the import or it will collide with packr
    gPacker "github.com/benweidig/giraffe/datasources/packr"
)

func yourSetupMethodMaybe() {
    r := gin.New()

    config := &giraffe.Config{
        Datasource:   &gPacker.Datasource{
            Box:        myPackrBox,
            Extensions: ".tpl",
        },
		Layout:       "master",
		Funcs:        make(template.FuncMap),
		DisableCache: false,
    }

    r.HTMLRender = giraffe.New(config)
}

Datasources

The giraffe uses a giraffe.Datasource to load the actual template content. Two datasources are included in the box:

fs.Datasource

import (
    "github.com/benweidig/giraffe/datasources/fs"
)

A simple filesystem datasource with 2 settings, but you could also use fs.Default() to get one with sensible defaults.

Setting Default() Description
Root views The root folder for the views
Extension .html The template extension (incl. the dot), so we can use shortes names

packr.Datasource

import (
    "github.com/benweidig/giraffe/datasources/packr"
)

A packr.Box-based datasource, no default is available.

Setting Description
Box The packr.Box
Extension The template extension (incl. the dot), so we can use shortes names

Convenience names

Templates are loaded by a more "convenient name" instead of the full/relative path. Partials are even shorter, you can ignore the partial folder name:

{{ partial "mypartial" . }}

To load the partial mypartial in the folder partials. Partials must reside in partials right now, so there's no need to write it all the time.

License

MIT. See LICENSE.

About

An HTML Renderer for Gin Gonic with support for layout, partials and datasources

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages