webhelp.v1: gopkg.in/webhelp.v1/whtmpl Index | Files

package whtmpl

import "gopkg.in/webhelp.v1/whtmpl"

Package whtmpl provides some helpful utilities for constructing and using lots of html/templates

Index

Package Files

tmpl.go

type Collection Uses

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

Collection is a useful type that helps when defining a bunch of html inside of Go files. Assuming you want to define a template called "landing" that references another template called "header". With a template collection, you would make three files:

pkg.go:

   package views

   import "gopkg.in/webhelp.v1/whtmpl"

   var Templates = whtmpl.NewCollection()

landing.go:

   package views

   var _ = Templates.MustParse(`{{ template "header" . }}

      <h1>Landing!</h1>`)

header.go:

   package views

   var _ = Templates.MustParse(`<title>My website!</title>`)

Note that MustParse determines the name of the template based on the go filename.

A template collection by default has two additional helper functions defined within templates:

* makemap: creates a map out of the even number of arguments given.
* makepair: creates a Pair type of its two given arguments.
* makeslice: creates a slice of the given arguments.
* safeurl: calls template.URL with its first argument and returns the
           result.

func NewCollection Uses

func NewCollection() *Collection

Creates a new Collection.

func (*Collection) Funcs Uses

func (tc *Collection) Funcs(m template.FuncMap) *Collection

Allows you to add and overwrite template function definitions. Mutates called collection and returns self.

func (*Collection) Lookup Uses

func (tc *Collection) Lookup(name string) *template.Template

Lookup a template by name. Returns nil if not found.

func (*Collection) MustParse Uses

func (tc *Collection) MustParse(tmpl string) *template.Template

MustParse parses template source "tmpl" and stores it in the Collection using the name of the go file that MustParse is called from.

func (*Collection) Parse Uses

func (tc *Collection) Parse(name string, tmpl string) (
    *template.Template, error)

Parse parses the source "tmpl" and stores it in the template collection using name "name".

func (*Collection) Render Uses

func (tc *Collection) Render(w http.ResponseWriter, r *http.Request,
    template string, values interface{})

Render writes the template out to the response writer (or any errors that come up), with value as the template value.

type Pair Uses

type Pair struct {
    First, Second interface{}
}

Pair is a useful type that allows for passing more than one current template variable into a sub-template.

Expected usage within a template like:

{{ template "subtemplate" (makepair $val1 $val2) }}

Expected usage within the subtemplate like

{{ $val1 := .First }}
{{ $val2 := .Second }}

"makepair" is registered as a template function inside a Collection

Package whtmpl imports 7 packages (graph) and is imported by 1 packages. Updated 2017-06-01. Refresh now. Tools for package owners.