sweetpl

package module
v0.0.0-...-330bfe2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2017 License: MIT Imports: 7 Imported by: 0

README

Sweetpl

Sugar for Go templates

This is a very small abstraction layer on top of the default golang template engine.

At this stage only http/template, but that's just because I'm a bit slack, and I'll fix that soon.

All existing template code will work, then the following are added:

Extends

{{ extends "/base.html" }}

{{ define "content" }}
  Blah
{{ end }}

Imports the named template.

The base golang template is the last one in the import chain. Rendering is done on the last 'extends' base in the chain, then the default package templating takes over.

Multiple and Optional Define Blocks

base.html

<!DOCTYPE html>
<html>
  <head>
    <title>{{ template "title" }}</title>
    {{ template "style" }}
  </head>
</html>

{{ define "title" }}Default Title{{ end }}

view.html

{{ extends "/base.html" }}

{{ define "title" }}Hello World{{ end }}

Code

tpl.Render(w, "view.html", nil)

Produces

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>

  </head>
</html>

Two things:

  • The 'title' block in view.html is used, it overrides the 'default' in the base. No errors.
  • The 'style' call in base.html is not defined anywhere, so it defaults to an empty string

Default data value for 'template' calls

In the standard package, the default value for "." when calling template is nil, in this package it defaults to the "." of the parent.

Auto func map import

The value of SweeTpl.FuncMap is injected into every layer of the template tree automatically.

Loaders

You can define your own loader - Interface sweetpl.TemplateLoader, has the method LoadTemplate(string) (string error) or there are two defined: Map loader - just a map[string]string of named template sources, and DirLoader which loads them by path from the filesystem (No checking for ../../../etc/ssh/keys yet, so be careful)

Usage:

tpl := sweetpl.SweeTpl{
	Loader: &sweetpl.DirLoader{
		BasePath: templatePath,
	},
	FuncMap: template.FuncMap{
		"asset": func(in string) string {
			return "/" + in
		},
	},
}

tpl.Render(w, "view.html", "Hello World")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errf

func Errf(format string, parameters ...interface{}) error

Types

type DirLoader

type DirLoader struct {
	BasePath string
}

func (*DirLoader) LoadTemplate

func (l *DirLoader) LoadTemplate(name string) (string, error)

DirLoader.LoadTemplate gets BaseAddress + name. No safety checking yet.

type MapLoader

type MapLoader map[string]string

func (*MapLoader) LoadTemplate

func (l *MapLoader) LoadTemplate(name string) (string, error)

MapLoader.LoadTemplate gets name from a map.

type NamedTemplate

type NamedTemplate struct {
	Name string
	Src  string
}

type SweeTpl

type SweeTpl struct {
	Loader  TemplateLoader
	FuncMap map[string]interface{} //template.FuncMap

	ForceReload bool
	// contains filtered or unexported fields
}

func (*SweeTpl) ClearCache

func (st *SweeTpl) ClearCache()

func (*SweeTpl) GetTemplate

func (st *SweeTpl) GetTemplate(w io.Writer, name string) (*template.Template, error)

func (*SweeTpl) Render

func (st *SweeTpl) Render(w io.Writer, name string, data interface{}) error

type TemplateData

type TemplateData struct {
	Title string
	Path  string
	User  interface{}
	Nav   map[string]string
	Data  map[string]interface{}
}

type TemplateError

type TemplateError struct {
	Format     string
	Parameters []interface{}
}

func (*TemplateError) Error

func (e *TemplateError) Error() string

type TemplateLoader

type TemplateLoader interface {
	LoadTemplate(string) (string, error)
}

Jump to

Keyboard shortcuts

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