render

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: MIT Imports: 13 Imported by: 40

Documentation

Overview

Package render is an internal package that renders a compiled template parse tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockCompiler

type BlockCompiler func(BlockNode) (func(io.Writer, Context) error, error)

BlockCompiler builds a renderer for the tag instance.

type BlockNode

type BlockNode struct {
	parser.Token

	Body    []Node
	Clauses []*BlockNode
	// contains filtered or unexported fields
}

BlockNode represents a {% tag %}…{% endtag %}.

type Config

type Config struct {
	parser.Config

	Cache map[string][]byte
	// contains filtered or unexported fields
}

Config holds configuration information for parsing and rendering.

func NewConfig

func NewConfig() Config

NewConfig creates a new Settings.

func (Config) AddBlock

func (g Config) AddBlock(name string) blockDefBuilder

AddBlock defines a control tag and its matching end tag.

func (*Config) AddTag

func (c *Config) AddTag(name string, td TagCompiler)

AddTag creates a tag definition.

func (Config) BlockSyntax

func (g Config) BlockSyntax(name string) (parser.BlockSyntax, bool)

BlockSyntax is part of the Grammar interface.

func (Config) Compile

func (c Config) Compile(source string, loc parser.SourceLoc) (Node, parser.Error)

Compile parses a source template. It returns an AST root, that can be evaluated.

func (*Config) FindTagDefinition

func (c *Config) FindTagDefinition(name string) (TagCompiler, bool)

FindTagDefinition looks up a tag definition.

type Context

type Context interface {
	// Get retrieves the value of a variable from the current lexical environment.
	Get(name string) interface{}
	// Errorf creates a SourceError, that includes the source location.
	// Use this to distinguish errors in the template from implementation errors
	// in the template engine.
	Errorf(format string, a ...interface{}) Error
	// Evaluate evaluates a compiled expression within the current lexical context.
	Evaluate(expressions.Expression) (interface{}, error)
	// EvaluateString compiles and evaluates a string expression such as “x”, “x < 10", or “a.b | split | first | default: 10”, within the current lexical context.
	EvaluateString(string) (interface{}, error)
	// ExpandTagArg renders the current tag argument string as a Liquid template.
	// It enables the implementation of tags such as Jekyll's "{% include {{ page.my_variable }} %}" andjekyll-avatar's  "{% avatar {{page.author}} %}".
	ExpandTagArg() (string, error)
	// InnerString is the rendered content of the current block.
	// It's used in the implementation of the Liquid "capture" tag and the Jekyll "highlght" tag.
	InnerString() (string, error)
	// RenderBlock is used in the implementation of the built-in control flow tags.
	// It's not guaranteed stable.
	RenderBlock(io.Writer, *BlockNode) error
	// RenderChildren is used in the implementation of the built-in control flow tags.
	// It's not guaranteed stable.
	RenderChildren(io.Writer) Error
	// RenderFile parses and renders a template. It's used in the implementation of the {% include %} tag.
	// RenderFile does not cache the compiled template.
	RenderFile(string, map[string]interface{}) (string, error)
	// Set updates the value of a variable in the current lexical environment.
	// It's used in the implementation of the {% assign %} and {% capture %} tags.
	Set(name string, value interface{})
	// SourceFile retrieves the value set by template.SetSourcePath.
	// It's used in the implementation of the {% include %} tag.
	SourceFile() string
	// TagArgs returns the text of the current tag, not including its name.
	// For example, the arguments to {% my_tag a b c %} would be “a b c”.
	TagArgs() string
	// TagName returns the name of the current tag; for example "my_tag" for {% my_tag a b c %}.
	TagName() string
	// WrapError creates a new error that records the source location from the current context.
	WrapError(err error) Error
}

Context provides the rendering context for a tag renderer.

type Error

type Error interface {
	Path() string
	LineNumber() int
	Cause() error
	Error() string
}

An Error is an error during template rendering.

func Render

func Render(node Node, w io.Writer, vars map[string]interface{}, c Config) Error

Render renders the render tree.

type Node

type Node interface {
	SourceLocation() parser.SourceLoc // for error reporting
	SourceText() string               // for error reporting
	// contains filtered or unexported methods
}

Node is a node of the render tree.

type ObjectNode

type ObjectNode struct {
	parser.Token
	// contains filtered or unexported fields
}

ObjectNode is an {{ object }} object.

type RawNode

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

RawNode holds the text between the start and end of a raw tag.

func (*RawNode) SourceLocation added in v0.2.0

func (n *RawNode) SourceLocation() parser.SourceLoc

func (*RawNode) SourceText added in v0.2.0

func (n *RawNode) SourceText() string

type SeqNode

type SeqNode struct {
	Children []Node
	// contains filtered or unexported fields
}

SeqNode is a sequence of nodes.

func (*SeqNode) SourceLocation added in v0.2.0

func (n *SeqNode) SourceLocation() parser.SourceLoc

func (*SeqNode) SourceText added in v0.2.0

func (n *SeqNode) SourceText() string

type TagCompiler

type TagCompiler func(expr string) (func(io.Writer, Context) error, error)

TagCompiler is a function that parses the tag arguments, and returns a renderer. TODO instead of using the bare function definition, use a structure that defines how to parse

type TagNode

type TagNode struct {
	parser.Token
	// contains filtered or unexported fields
}

TagNode renders itself via a render function that is created during parsing.

type TextNode

type TextNode struct {
	parser.Token
}

TextNode is a text chunk, that is rendered verbatim.

Jump to

Keyboard shortcuts

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