whtmpl

package
Version: v1.0.0-...-3f30213 Latest Latest
Warning

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

Go to latest
Published: May 30, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

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

func NewCollection() *Collection

Creates a new Collection.

func (*Collection) Funcs

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

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

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

func (*Collection) MustParse

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

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

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

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

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL