Documentation ¶
Overview ¶
Package parser defines an interface for parsers which the base template conforms to
Package parser defines an interface for parsers (creating templates) and templates (rendering content), and defines a base template type which conforms to both interfaces and can be included in any templates
Index ¶
- Variables
- type BaseTemplate
- func (t *BaseTemplate) CacheKey() string
- func (t *BaseTemplate) CanParseFile(path string) bool
- func (t *BaseTemplate) Dependencies() []Template
- func (t *BaseTemplate) Finalize(templates map[string]Template) error
- func (t *BaseTemplate) NewTemplate(fullpath, path string) (Template, error)
- func (t *BaseTemplate) Parse() error
- func (t *BaseTemplate) ParseString(s string) error
- func (t *BaseTemplate) Path() string
- func (t *BaseTemplate) Render(writer io.Writer, context map[string]interface{}) error
- func (t *BaseTemplate) Setup(viewsPath string, helpers FuncMap) error
- func (t *BaseTemplate) Source() string
- type FuncMap
- type HTMLTemplate
- func (t *HTMLTemplate) CanParseFile(path string) bool
- func (t *HTMLTemplate) Finalize(templates map[string]Template) error
- func (t *HTMLTemplate) NewTemplate(fullpath, path string) (Template, error)
- func (t *HTMLTemplate) Parse() error
- func (t *HTMLTemplate) ParseString(s string) error
- func (t *HTMLTemplate) Render(writer io.Writer, context map[string]interface{}) error
- func (t *HTMLTemplate) Setup(helpers FuncMap) error
- type JSONTemplate
- func (t *JSONTemplate) CanParseFile(path string) bool
- func (t *JSONTemplate) Finalize(templates map[string]Template) error
- func (t *JSONTemplate) NewTemplate(fullpath, path string) (Template, error)
- func (t *JSONTemplate) Parse() error
- func (t *JSONTemplate) ParseString(s string) error
- func (t *JSONTemplate) Render(writer io.Writer, context map[string]interface{}) error
- func (t *JSONTemplate) Setup(helpers FuncMap) error
- type Parser
- type Scanner
- type Template
- type TextTemplate
- func (t *TextTemplate) CanParseFile(path string) bool
- func (t *TextTemplate) Finalize(templates map[string]Template) error
- func (t *TextTemplate) NewTemplate(fullpath, path string) (Template, error)
- func (t *TextTemplate) Parse() error
- func (t *TextTemplate) ParseString(s string) error
- func (t *TextTemplate) Render(writer io.Writer, context map[string]interface{}) error
- func (t *TextTemplate) Setup(helpers FuncMap) error
Constants ¶
This section is empty.
Variables ¶
var MaxCacheKeyLength = 250
MaxCacheKeyLength determines the max key length for cache keys
Functions ¶
This section is empty.
Types ¶
type BaseTemplate ¶
type BaseTemplate struct {
// contains filtered or unexported fields
}
BaseTemplate is a base template which conforms to Template and Parser interfaces. This is an abstract base type, we use html or text templates
func (*BaseTemplate) CacheKey ¶
func (t *BaseTemplate) CacheKey() string
CacheKey returns the cache key of this template - (this is generated from path + hash of contents + dependency hash keys). So it automatically changes when templates are changed
func (*BaseTemplate) CanParseFile ¶
func (t *BaseTemplate) CanParseFile(path string) bool
CanParseFile returns true if we can parse this file
func (*BaseTemplate) Dependencies ¶
func (t *BaseTemplate) Dependencies() []Template
Dependencies returns which other templates this one depends on (for generating nested cache keys)
func (*BaseTemplate) Finalize ¶
func (t *BaseTemplate) Finalize(templates map[string]Template) error
Finalize is called on each template after parsing is finished, supplying complete template set.
func (*BaseTemplate) NewTemplate ¶
func (t *BaseTemplate) NewTemplate(fullpath, path string) (Template, error)
NewTemplate returns a newly created template for this path
func (*BaseTemplate) Parse ¶
func (t *BaseTemplate) Parse() error
Parse the template (BaseTemplate simply stores it)
func (*BaseTemplate) ParseString ¶
func (t *BaseTemplate) ParseString(s string) error
ParseString a string template
func (*BaseTemplate) Path ¶
func (t *BaseTemplate) Path() string
Path returns the path of this template
func (*BaseTemplate) Render ¶
func (t *BaseTemplate) Render(writer io.Writer, context map[string]interface{}) error
Render the template ignoring context
func (*BaseTemplate) Setup ¶
func (t *BaseTemplate) Setup(viewsPath string, helpers FuncMap) error
Setup sets up the template for parsing
func (*BaseTemplate) Source ¶
func (t *BaseTemplate) Source() string
Source the parsed version of this template
type HTMLTemplate ¶
type HTMLTemplate struct {
BaseTemplate
}
HTMLTemplate represents an HTML template using go HTML/template
func (*HTMLTemplate) CanParseFile ¶
func (t *HTMLTemplate) CanParseFile(path string) bool
CanParseFile returns true if this parser handles this path
func (*HTMLTemplate) Finalize ¶
func (t *HTMLTemplate) Finalize(templates map[string]Template) error
Finalize the template set, called after parsing is complete
func (*HTMLTemplate) NewTemplate ¶
func (t *HTMLTemplate) NewTemplate(fullpath, path string) (Template, error)
NewTemplate returns a new template for this type
func (*HTMLTemplate) ParseString ¶
func (t *HTMLTemplate) ParseString(s string) error
ParseString parses a string template
func (*HTMLTemplate) Render ¶
func (t *HTMLTemplate) Render(writer io.Writer, context map[string]interface{}) error
Render the template to the given writer, returning an error
func (*HTMLTemplate) Setup ¶
func (t *HTMLTemplate) Setup(helpers FuncMap) error
Setup performs setup before parsing templates
type JSONTemplate ¶
type JSONTemplate struct {
BaseTemplate
}
JSONTemplate represents a template using go HTML/template
func (*JSONTemplate) CanParseFile ¶
func (t *JSONTemplate) CanParseFile(path string) bool
CanParseFile returns true if this template can parse this file
func (*JSONTemplate) Finalize ¶
func (t *JSONTemplate) Finalize(templates map[string]Template) error
Finalize the template set, called after parsing is complete
func (*JSONTemplate) NewTemplate ¶
func (t *JSONTemplate) NewTemplate(fullpath, path string) (Template, error)
NewTemplate returns a new JSONTemplate
func (*JSONTemplate) ParseString ¶
func (t *JSONTemplate) ParseString(s string) error
ParseString parses a string template
func (*JSONTemplate) Render ¶
func (t *JSONTemplate) Render(writer io.Writer, context map[string]interface{}) error
Render the template
func (*JSONTemplate) Setup ¶
func (t *JSONTemplate) Setup(helpers FuncMap) error
Setup performs one-time setup before parsing templates
type Parser ¶
type Parser interface { // Setup is called once on setup of a parser Setup(helpers FuncMap) error // Can this parser handle this file? CanParseFile(path string) bool // Parse the file given and return a compiled template NewTemplate(fullpath, path string) (Template, error) }
Parser loads template files, and returns a template suitable for rendering content
type Scanner ¶
type Scanner struct { // A map of all templates keyed by path name Templates map[string]Template // A set of parsers (in order) with which to parse templates Parsers []Parser // A set of paths (in order) from which to load templates Paths []string // Helpers is a list of helper functions Helpers FuncMap // contains filtered or unexported fields }
Scanner scans paths for templates and creates a representation of each using parsers
func NewScanner ¶
NewScanner creates a new template scanner
type Template ¶
type Template interface { // Parse a template file Parse() error // Called after parsing is finished Finalize(templates map[string]Template) error // Render to this writer Render(writer io.Writer, context map[string]interface{}) error // Return the original template content Source() string // Return the template path Path() string // Return the cache key CacheKey() string // Return dependencies of this template (used for creating cache keys) Dependencies() []Template }
Template renders its content given a ViewContext
type TextTemplate ¶
type TextTemplate struct {
BaseTemplate
}
TextTemplate using go text/template
func (*TextTemplate) CanParseFile ¶
func (t *TextTemplate) CanParseFile(path string) bool
CanParseFile returns true if this parser handles this file path?
func (*TextTemplate) Finalize ¶
func (t *TextTemplate) Finalize(templates map[string]Template) error
Finalize the template set, called after parsing is complete Record a list of dependent templates (for breaking caches automatically)
func (*TextTemplate) NewTemplate ¶
func (t *TextTemplate) NewTemplate(fullpath, path string) (Template, error)
NewTemplate returns a new template of this type
func (*TextTemplate) ParseString ¶
func (t *TextTemplate) ParseString(s string) error
ParseString a string template
func (*TextTemplate) Render ¶
func (t *TextTemplate) Render(writer io.Writer, context map[string]interface{}) error
Render renders the template
func (*TextTemplate) Setup ¶
func (t *TextTemplate) Setup(helpers FuncMap) error
Setup runs before parsing templates