giraffe

package module
v0.0.0-...-6603c43 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2018 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Datasource   Datasource       // The datasource which loads the template content
	Layout       string           // Filename / path to layout (without extension)
	Funcs        template.FuncMap // Template functions
	DisableCache bool             // Disables caching
}

Config is the config of the template engine

type Datasource

type Datasource interface {
	// LoadContent should load a template by its "convenience name"
	LoadContent(name string) (string, error)
}

Datasource is an abstraction of how to load template content

type Giraffe

type Giraffe struct {
	// contains filtered or unexported fields
}

Giraffe is the main struct holding it all together

func Debug

func Debug() *Giraffe

Debug creates a new Giraffe for debugging

func Default

func Default() *Giraffe

Default creates a new Giraffe with sensible defaults

func New

func New(config Config) *Giraffe

New creates a new Giraffe but you have to bring your own config

func (*Giraffe) PrepareTemplateFuncs

func (g *Giraffe) PrepareTemplateFuncs()

PrepareTemplateFuncs adds the "partial" func and the user provided funcs to Giraffe

func (*Giraffe) Render

func (g *Giraffe) Render(out io.Writer, name string, data interface{}, useLayout bool) error

Render does what it's called, it renders a template with the provided data. It supports caching (if not disabled) and the provided funcs.

Directories

Path Synopsis
datasources
fs

Jump to

Keyboard shortcuts

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