Help us shape the future of Go! Take the 2026 Go Developer Survey

temple

package module
v0.0.0-...-37dc5a8 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2015 License: MIT Imports: 7 Imported by: 0

README

temple

Convention based template abstraction for Golang.

  • Each directory is a template made up of many partials
  • Ancestors are inherited and available to children
  • Partials are available to every template
  • Auto-reload: Recompiles templates whenever a dependent file changes
  • Name a file base.temple to create master templates
  • Use your own file extensions (as long as .temple appears)

Get started

Create a file structure that looks something like this:

/templates
  /home                 // tpl.Get("home")
    /welcome            // tpl.Get("home.welcome")
      content.temple
  /about                // tpl.Get("about")
    content.temple
  base.temple           // base file for everything
  copyright.temple      // partial available to base, welcome and about
  /_punctuation
    period.temple       // shared component

Insert the following

base.temple
<div>
{{ template "content" . }}
</div>
home/welcome/content.temple
Welcome to the site {{ template "copyright" }}
about/content.temple
{{ template "copyright" }} All about the site{{ template "punctuation.period" }}
(Copyright &copy; 2015)
Using temple

First, process the files by calling temple.New:

tpl, err := temple.New("/path/to/templates")
if err != nil {
	log.Fatalln("fatal error:", err)
}

Then use the tpl as a map, using dot notation to access the templates, and call the Execute method:

http.HandleFunc("/home/welcome", func(w http.ResponseWriter, r *http.Request) {
	if err := tpl.Get("home.welcome").Execute(w, nil); err != nil {
    // TODO: handle error
  }
})

Using the reloader

Use temple.New as normal:

tpl, err := temple.New("/path/to/templates")
if err != nil {
  log.Fatalln("fatal error:", err)
}

Then mix in a temple/reloader:

rl, err := reloader.New(tpl)
if err != nil {
  log.Fatalln("fatal error:", err)
}
defer rl.Close() // be sure to close it

Now whenever a template file changes, the reloader will call the Reload method.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Template

type Template struct {
	*template.Template

	// RootTemplateName is the name of the template that will be
	// rendered when Execute is called.
	RootTemplateName string
	// Files represents the files that make up this template.
	Files []string
	// contains filtered or unexported fields
}

Template represents a single temple Template.

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data interface{}) error

Execute applies a parsed template to the specified data object, writing the output to wr. It calls ExecuteTemplate on the underlying Template with the appropriate RootTemplateName.

type Temple

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

Temple represents a map of Templates keyed by their dot notation names.

func New

func New(root string) (*Temple, error)

New walks directories starting at root and generates a Temple object containing all compiled templates.

func NewOnTemplate

func NewOnTemplate(root string, onTemplate func(template *template.Template) (*template.Template, error)) (*Temple, error)

NewOnTemplate walks directories starting at root and generates a Temple object containing all compiled templates, calling the OnTemplate callback for each template.

func (*Temple) Files

func (t *Temple) Files() []string

Files gets the template files that were loaded.

func (*Temple) Get

func (t *Temple) Get(name string) *Template

Get gets a Template by name.

func (*Temple) GetOK

func (t *Temple) GetOK(name string) (*Template, bool)

GetOK gets a Template by name and returns whether one was found or not.

func (*Temple) Reload

func (t *Temple) Reload() error

Reload reloads all templates.

func (*Temple) Root

func (t *Temple) Root() string

Root gets the root directory for the templates.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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