bip

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2017 License: MIT Imports: 2 Imported by: 0

README

bip

Wrapper of pongo2.TemplateSet for go-bindata

GoDoc

Motivation

When use go-bindata and pongo2 in combination, it is troublesome to build every time rewrite views (even using Must). So bip behaves what read from files every executions when in development, and read from bindata when in production.

Usage

package main

import (
  ...
)

var tplSet *bip.TemplateSet
var indexTpl bip.Template

func init() {
  tplSet = bip.NewSet(asset.Asset) // asset is a package generated from bindata.
  indexTpl = bip.Must(tplSet.FromFile("index.html"))
}

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    if err := indexTpl.ExecuteWriter(bip.Context{}, w); err != nil {
      fmt.Fprintf(w, err.Error())
    }
  })
  http.ListenAndServe(":8080", nil)
}

if you use as production binary, you need to add bip to build tag.

$ go build -tags="bip ...other tags"

Caution

bip depends on master of pongo2. DO NOT use v3.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context pongo2.Context

Context alias pongo2.Context. Context provides constants, variables, instances or functions to a template. see https://godoc.org/github.com/flosch/pongo2#Context

type FileTemplate

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

FileTemplate ...

func (*FileTemplate) Execute

func (t *FileTemplate) Execute(context Context) (string, error)

Execute the template and returns the rendered template as a string.

func (*FileTemplate) ExecuteBytes

func (t *FileTemplate) ExecuteBytes(context Context) ([]byte, error)

ExecuteBytes the template and returns the rendered template as a []byte.

func (*FileTemplate) ExecuteWriter

func (t *FileTemplate) ExecuteWriter(context Context, w io.Writer) error

ExecuteWriter writes result that the template with the given context to writer.

type Template

type Template interface {
	Execute(context Context) (string, error)
	ExecuteBytes(context Context) ([]byte, error)
	ExecuteWriter(context Context, w io.Writer) error
}

Template ...

func Must

func Must(tpl Template, err error) Template

Must raises panic, if a Template couldn't successfully parsed.

type TemplateSet

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

TemplateSet is group of template.

func NewSet

func NewSet(asset func(path string) ([]byte, error)) *TemplateSet

NewSet create TemplateSet using bindata.

func (*TemplateSet) FromFile

func (ts *TemplateSet) FromFile(filename string) (Template, error)

FromFile loads a template from a filename and returns a Template instance.

Jump to

Keyboard shortcuts

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