template

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockNode

type BlockNode struct {
	TagName  string
	TagArgs  string
	Children NodeList
	// Used for blocks that have alternative branches like {% else %}
	ElseChildren NodeList
}

BlockNode represents a built-in block like {% if %} or custom blocks.

func (*BlockNode) Render

func (n *BlockNode) Render(ctx *Context) (string, error)

type Context

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

Context represents a stack of dictionaries for template rendering.

func NewContext

func NewContext(data map[string]any) *Context

NewContext creates a new context with an initial dictionary.

func (*Context) Flatten

func (c *Context) Flatten() map[string]any

Flatten combines all dictionaries in the stack into a single map. Values higher in the stack take precedence.

func (*Context) Get

func (c *Context) Get(key string) (any, bool)

Get retrieves a value from the context, searching from top to bottom.

func (*Context) GetEngine

func (c *Context) GetEngine() *Engine

GetEngine fetches the Engine pointer if attached.

func (*Context) GetRenderState

func (c *Context) GetRenderState(key string) any

GetRenderState fetches internal engine state data.

func (*Context) Pop

func (c *Context) Pop() map[string]any

Pop removes the top dictionary from the stack.

func (*Context) Push

func (c *Context) Push(data map[string]any)

Push adds a new dictionary to the stack.

func (*Context) Resolve

func (c *Context) Resolve(path string) any

Resolve allows external packages (like tags) to resolve variables using the private resolver.

func (*Context) Set

func (c *Context) Set(key string, value any)

Set sets a value in the top dictionary of the stack.

func (*Context) SetRenderState

func (c *Context) SetRenderState(key string, val any)

SetRenderState saves internal engine state data.

type Engine

type Engine struct {
	DIRS       []string
	APP_DIRS   bool
	Autoescape bool
	Libraries  map[string]*Library
	Builtins   []*Library
	Loaders    []loaders.Loader
	// contains filtered or unexported fields
}

Engine is responsible for loading and rendering templates.

func NewEngine

func NewEngine(dirs []string, appDirs bool) *Engine

NewEngine creates a new template engine.

func (*Engine) AddBuiltin

func (e *Engine) AddBuiltin(lib *Library)

AddBuiltin adds a library that is always loaded

func (*Engine) FromString

func (e *Engine) FromString(content string) (*Template, error)

FromString compiles a template directly from a string.

func (*Engine) GetTemplate

func (e *Engine) GetTemplate(name string) (*Template, error)

GetTemplate loads a template by name, optionally from cache.

func (*Engine) RegisterLibrary

func (e *Engine) RegisterLibrary(name string, lib *Library)

RegisterLibrary makes a library available for `{% load %}`

type FilterFunc

type FilterFunc func(val any, args string) (any, error)

FilterFunc is a function that filters a template variable.

type Lexer

type Lexer struct {
	Tokens []Token
	// contains filtered or unexported fields
}

Lexer breaks a template string into tokens.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer.

func (*Lexer) Lex

func (l *Lexer) Lex() []Token

Lex scans the input and produces tokens.

type Library

type Library struct {
	Tags    map[string]TagParser
	Filters map[string]FilterFunc
}

Library represents a collection of tags and filters.

func NewLibrary

func NewLibrary() *Library

NewLibrary creates a new Library.

func (*Library) RegisterFilter

func (l *Library) RegisterFilter(name string, filter FilterFunc)

RegisterFilter registers a filter function.

func (*Library) RegisterTag

func (l *Library) RegisterTag(name string, parser TagParser)

RegisterTag registers a tag parser.

type Node

type Node interface {
	Render(ctx *Context) (string, error)
}

Node represents a parsed element of a template.

type NodeList

type NodeList []Node

NodeList is a collection of nodes.

func (NodeList) Render

func (nl NodeList) Render(ctx *Context) (string, error)

type Parser

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

Parser builds an AST from tokens.

func NewParser

func NewParser(tokens []Token, engine *Engine) *Parser

NewParser creates a new parser.

func (*Parser) GetEngine

func (p *Parser) GetEngine() *Engine

GetEngine fetches Engine attached to parser

func (*Parser) NextToken

func (p *Parser) NextToken() *Token

NextToken consumes and returns the next token.

func (*Parser) Parse

func (p *Parser) Parse(stopAt []string) (NodeList, error)

Parse runs the parser until it runs out of tokens or hits a stop token.

func (*Parser) PrependToken

func (p *Parser) PrependToken(tok Token)

PrependToken puts a token back into the stream.

func (*Parser) RegisterTag

func (p *Parser) RegisterTag(name string, parser TagParser)

RegisterTag allows a tag to register tags to the parser dynamically (e.g. {% load %})

type SafeString

type SafeString string

SafeString represents a string that has already been safely HTML-escaped and should bypass autoescaping during rendering.

type TagParser

type TagParser func(parser *Parser, token Token) (Node, error)

TagParser is a function that parses a specific template tag.

type Template

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

Template represents a parsed template.

func (*Template) Name

func (t *Template) Name() string

func (*Template) Render

func (t *Template) Render(ctx *Context) (string, error)

Render renders the template with a given context.

func (*Template) RenderToResponse

func (t *Template) RenderToResponse(req *godjangohttp.Request, ctx *Context) godjangohttp.Response

RenderToResponse renders the template and returns an HttpResponse.

type TextNode

type TextNode struct {
	Text string
}

TextNode renders static text.

func (*TextNode) Render

func (n *TextNode) Render(ctx *Context) (string, error)

type Token

type Token struct {
	Type     TokenType
	Contents string
	Line     int
}

Token represents a lexical token in a template.

type TokenType

type TokenType int

TokenType represents the type of a template token.

const (
	TokenText TokenType = iota
	TokenVar
	TokenBlock
	TokenComment
)

type VariableNode

type VariableNode struct {
	Expression string
}

VariableNode renders a context variable, applying filters.

func (*VariableNode) Render

func (n *VariableNode) Render(ctx *Context) (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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