loadtmpl

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2017 License: MIT Imports: 5 Imported by: 0

README

go-loadtmpl

Load standard library HTML templates from an http.Filesystem while adding inheritance using a special comment that indicates the template's parent.

Example

First define your base template, e.g layout.html:

<html>
  <body>
    <h1>Page {{block "page_title" .}}{{end}}</h1>
    <div>
      {{block "page" .}}{{end}}
    </div>
  </body>
</html>

Then, write the template that extends it and add the specially crafted comment at the top:

{{/* extends "layout.html" */}}
{{block "page_title" .}}About{{end}}
{{block "page" .}}I load templates{{end}}

In you Go file, probably somewhere next to your web handler

fs := http.Dir(".") //provide access to the template files through the http.FileSystem interface.
l := loadtmpl.New(fs)
t, _ := l.Load("about.html") //load the template, its base template will automatically loaded

t, _ = l.Load("about.html") //second calls are retrieved from cache

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchParentComment

func MatchParentComment(data []byte) string

MatchParentComment will search data for the comment that defines the parent it extends. If match is empty is means no parent is defined

Types

type Loader

type Loader struct {

	//NoCache disables templates being loaded from cache
	NoCache bool
	// contains filtered or unexported fields
}

Loader will fetch templates by name from an http.FileSystem. It provides template inheritance by search the loaded template for a specially crafted comment. Loading of templates with the same name are cached and not loaded calls

func New

func New(fs http.FileSystem, funcs template.FuncMap) *Loader

New loader will be setup

func (*Loader) Load

func (l *Loader) Load(name string) (t *template.Template, err error)

Load a template and cache it.

Jump to

Keyboard shortcuts

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