parser

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2015 License: MIT Imports: 12 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
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) FinishParse

func (t *BaseTemplate) FinishParse(viewsPath string) error

FinishParse finishes parsing this path

func (*BaseTemplate) NewTemplate

func (t *BaseTemplate) NewTemplate(path string) (Template, error)

NewTemplate returns a newly created template for this path

func (*BaseTemplate) Parse

func (t *BaseTemplate) Parse(path string) 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) Source

func (t *BaseTemplate) Source() string

Source the parsed version of this template

func (*BaseTemplate) StartParse

func (t *BaseTemplate) StartParse(viewsPath string, helpers FuncMap) error

StartParse starts parsing - the default implementation does nothing

type FuncMap

type FuncMap map[string]interface{}

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(path string) (Template, error)

NewTemplate returns a new template for this type

func (*HTMLTemplate) Parse

func (t *HTMLTemplate) Parse(path string) error

Parse the template at path

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) StartParse

func (t *HTMLTemplate) StartParse(viewsPath string, helpers FuncMap) error

StartParse 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(path string) (Template, error)

NewTemplate returns a new JSONTemplate

func (*JSONTemplate) Parse

func (t *JSONTemplate) Parse(path string) error

Parse the template

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) StartParse

func (t *JSONTemplate) StartParse(viewsPath string, helpers FuncMap) error

StartParse performs one-time setup before parsing templates

type Parser

type Parser interface {
	// Called before parsing commences for viewsPath
	StartParse(viewsPath string, helpers FuncMap) error

	// Can this parser handle this file?
	CanParseFile(path string) bool

	// Parse the file given and return a compiled template
	NewTemplate(path string) (Template, error)

	// Called when parsing finishes for viewsPath
	FinishParse(viewsPath string) error
}

A 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
}

Scanner scans paths for templates and creates a representation of each using parsers

func NewScanner

func NewScanner() (*Scanner, error)

NewScanner creates a new template scanner

func (*Scanner) RescanPaths

func (s *Scanner) RescanPaths(helpers FuncMap) error

RescanPaths rescans all template paths

func (*Scanner) ScanPath

func (s *Scanner) ScanPath(root string, helpers FuncMap) error

ScanPath scans a path for template files, including sub-paths

type Template

type Template interface {
	// Parse a template file
	Parse(path string) 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
}

A HTML template using go HTML/template - NB this is not escaped and unsafe in HTML.

func (*TextTemplate) CanParseFile

func (t *TextTemplate) CanParseFile(path string) bool

Can this parser handle this file path? test.csv.gotext

func (*TextTemplate) Finalize

func (t *TextTemplate) Finalize(templates map[string]Template) error

Finalise 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(path string) (Template, error)

func (*TextTemplate) Parse

func (t *TextTemplate) Parse(path string) error

Parse the template

func (*TextTemplate) ParseString

func (t *TextTemplate) ParseString(s string) error

Parse a string template

func (*TextTemplate) Render

func (t *TextTemplate) Render(writer io.Writer, context map[string]interface{}) error

Render renders the template

func (*TextTemplate) StartParse

func (t *TextTemplate) StartParse(viewsPath string, helpers FuncMap) error

Perform setup before parsing templates

Jump to

Keyboard shortcuts

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