parser

package
v0.0.0-...-a11cca7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: AGPL-3.0, MIT Imports: 12 Imported by: 0

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

type FuncMap map[string]interface{}

FuncMap is a map of functions

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

func (t *HTMLTemplate) Parse() 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) 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) Parse

func (t *JSONTemplate) Parse() 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) 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

func NewScanner(paths []string, helpers FuncMap) (*Scanner, error)

NewScanner creates a new template scanner

func (*Scanner) ScanPath

func (s *Scanner) ScanPath(root string) error

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

func (*Scanner) ScanPaths

func (s *Scanner) ScanPaths() error

ScanPaths resets template list and rescans all template paths

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

func (t *TextTemplate) Parse() error

Parse the template

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

Jump to

Keyboard shortcuts

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