environment

package
v0.0.0-...-a151dae Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = map[string]Test{
	"odd":         testOdd,
	"even":        testEven,
	"divisibleby": testDivisibleBy,
	"defined":     testDefined,
	"undefined":   testUndefined,
	"filter":      testFilter,
	"test":        testTest,
	"none":        testNone,
	"boolean":     testBoolean,
	"false":       testFalse,
	"true":        testTrue,
	"integer":     testInteger,
	"float":       testFloat,
	"lower":       testLower,
	"upper":       testUpper,
	"string":      testString,
	"mapping":     testMapping,
	"number":      testNumber,
	"sequence":    testSequence,
	"iterable":    testIterable,
	"callable":    testCallable,
	"sameas":      testSameAs,
	"escaped":     testEscaped,
	"in":          testIn,
	"==":          wrapOperator(operator.Eq),
	"eq":          wrapOperator(operator.Eq),
	"equalto":     wrapOperator(operator.Eq),
	"!=":          wrapOperator(operator.Ne),
	"ne":          wrapOperator(operator.Ne),
	">":           wrapOperator(operator.Gt),
	"gt":          wrapOperator(operator.Gt),
	"greaterthan": wrapOperator(operator.Gt),
	"<":           wrapOperator(operator.Lt),
	"lt":          wrapOperator(operator.Lt),
	"lessthan":    wrapOperator(operator.Lt),
	"<=":          wrapOperator(operator.Le),
	"le":          wrapOperator(operator.Le),
	">=":          wrapOperator(operator.Ge),
	"ge":          wrapOperator(operator.Ge),
}

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Add(key, value interface{}) (evicted bool)
	Get(key interface{}) (value interface{}, ok bool)
}

type Class

type Class struct{}

func (Class) FromSource

func (Class) FromSource(env *Environment, source string, filename *string, globals map[string]any, upToData UpToDate) (ITemplate, error)

type EnvOpts

type EnvOpts struct {
	*lexer.EnvLexerInformation
	Optimized  bool
	Extensions map[string]func(*Environment) extensions.IExtension // TODO jinja accepts also extensions names but it's python import magic I don't know how to do it in golang.
	Undefined  UndefinedConstructor
	Finalize   func(...any) any
	AutoEscape any // bool or func(string)bool
	Loader     *Loader
	CacheSize  int
	AutoReload bool
}

func DefaultEnvOpts

func DefaultEnvOpts() *EnvOpts

type Environment

type Environment struct {
	Sandboxed     bool
	Overlayed     bool
	LinkedTo      *Environment
	Shared        bool
	Concat        func([]string) string
	ContextClass  runtime.ContextClass
	TemplateClass Class
	*lexer.EnvLexerInformation
	Optimized  bool
	Extensions ExtensionsMap
	Undefined  UndefinedConstructor
	Finalize   func(...any) any
	AutoEscape func(name string) bool
	Loader     *Loader
	Cache      Cache
	AutoReload bool
	Filters    map[string]filters.Filter
	Tests      map[string]Test
	Globals    map[string]any
	Policies   map[string]any
}

Environment is the core component of Jinja. It contains important shared variables like configuration, filters, tests, globals and others. Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.

func New

func New(opts *EnvOpts) (*Environment, error)

func (*Environment) GetTemplate

func (env *Environment) GetTemplate(name any, parent *string, globals map[string]any) (ITemplate, error)

GetTemplate loads a template by name with `Loader` and returns a `Template`. If the template does not exist a `TemplateNotFound` exception is raised.

func (*Environment) JoinPath

func (env *Environment) JoinPath(v string, parent string) string

JoinPath joins a template with the parent. By default, all the lookups are relative to the loader root so this method returns the `template` parameter unchanged, but if the paths should be relative to the parent template, this function can be used to calculate the real template name.

Subclasses may override this method and implement template path joining here.

func (*Environment) MakeGlobals

func (env *Environment) MakeGlobals(globals map[string]any) map[string]any

type Escaped

type Escaped interface {
	HTML() (string, error)
}

type ExtensionsMap

type ExtensionsMap map[string]extensions.IExtension

func LoadExtensions

func LoadExtensions(env *Environment, extensions map[string]func(*Environment) extensions.IExtension) ExtensionsMap

type ITemplate

type ITemplate interface {
	IsUpToDate() bool
	Globals() map[string]any
}

type Loader

type Loader struct {
	LoaderEmbed
}

func NewFileSystemLoader

func NewFileSystemLoader[S utils.StrOrSlice](searchPath S, encoding string, followLinks bool) *Loader

func (*Loader) Load

func (l *Loader) Load(env *Environment, name string, globals map[string]any) (ITemplate, error)

type LoaderEmbed

type LoaderEmbed interface {
	HasSourceAccess() bool
	GetSource(env *Environment, template string) (string, *string, UpToDate, error)
	ListTemplates() ([]string, error)
}

type Template

type Template struct{}

type Test

type Test func(env *Environment, firstArg any, args ...any) (bool, error)

Test represents a test function. Some tests only require one variable

type UndefinedConstructor

type UndefinedConstructor func(hint *string, obj any, name *string, exc func(msg string) error, logger *log.Logger) runtime.IUndefined

type UpToDate

type UpToDate = func() bool

Jump to

Keyboard shortcuts

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