Documentation
¶
Overview ¶
Package easytemplate provides a templating engine with super powers, that allows templates to be written in go text/template syntax, with the ability to run javascript snippets in the template, and control further templating from within the javascript or other templates.
Index ¶
- Variables
- type CallContext
- type Engine
- func (e *Engine) RunMethod(scriptFile string, data any, fnName string, args ...any) (goja.Value, error)
- func (e *Engine) RunMethodWithContext(ctx context.Context, scriptFile string, data any, fnName string, args ...any) (goja.Value, error)
- func (e *Engine) RunScript(scriptFile string, data any) error
- func (e *Engine) RunScriptWithContext(ctx context.Context, scriptFile string, data any) error
- func (e *Engine) RunTemplate(templateFile string, outFile string, data any) error
- func (e *Engine) RunTemplateString(templateFile string, data any) (string, error)
- func (e *Engine) RunTemplateStringInput(name, template string, data any) (string, error)
- func (e *Engine) RunTemplateStringInputWithContext(ctx context.Context, name, template string, data any) (string, error)
- func (e *Engine) RunTemplateStringWithContext(ctx context.Context, templateFile string, data any) (string, error)
- func (e *Engine) RunTemplateWithContext(ctx context.Context, templateFile string, outFile string, data any) error
- type Opt
- func WithDebug() Opt
- func WithJSFiles(files map[string]string) Opt
- func WithJSFuncs(funcs map[string]func(call CallContext) goja.Value) Opt
- func WithReadFileSystem(fs fs.FS) Opt
- func WithSearchLocations(searchLocations []string) Opt
- func WithTemplateFuncs(funcs map[string]any) Opt
- func WithTracer(t trace.Tracer) Opt
- func WithWriteFunc(writeFunc func(string, []byte) error) Opt
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlreadyRan is returned when the engine has already been ran, and can't be ran again. In order to run the engine again, a new engine must be created. ErrAlreadyRan = errors.New("engine has already been ran") // ErrReserved is returned when a template or js function is reserved and can't be overridden. ErrReserved = errors.New("function is a reserved function and can't be overridden") // ErrInvalidArg is returned when an invalid argument is passed to a function. ErrInvalidArg = errors.New("invalid argument") // ErrTemplateCompilation is returned when a template fails to compile. ErrTemplateCompilation = errors.New("template compilation failed") // ErrFunctionNotFound Function does not exist in script. ErrFunctionNotFound = errors.New("failed to find function") )
Functions ¶
This section is empty.
Types ¶
type CallContext ¶ added in v0.3.0
type CallContext struct { goja.FunctionCall VM *vm.VM Ctx context.Context //nolint:containedctx // runtime context is necessarily stored in a struct as it jumps from Go to JS. }
CallContext is the context that is passed to go functions when called from js.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine provides the templating engine.
func (*Engine) RunMethod ¶ added in v0.7.5
func (e *Engine) RunMethod(scriptFile string, data any, fnName string, args ...any) (goja.Value, error)
RunMethod enables calls to global template methods from easytemplate.
func (*Engine) RunMethodWithContext ¶ added in v0.9.0
func (e *Engine) RunMethodWithContext(ctx context.Context, scriptFile string, data any, fnName string, args ...any) (goja.Value, error)
RunMethodWithContext enables calls to global template methods from easytemplate.
func (*Engine) RunScript ¶
RunScript runs the provided script file, with the provided data, starting the template engine and templating any templates triggered from the script.
func (*Engine) RunScriptWithContext ¶ added in v0.9.0
RunScriptWithContext runs the provided script file, with the provided data, starting the template engine and templating any templates triggered from the script.
func (*Engine) RunTemplate ¶
RunTemplate runs the provided template file, with the provided data, starting the template engine and templating the provided template to a file.
func (*Engine) RunTemplateString ¶
RunTemplateString runs the provided template file, with the provided data, starting the template engine and templating the provided template, returning the rendered result.
func (*Engine) RunTemplateStringInput ¶ added in v0.5.0
RunTemplateStringInput runs the provided input template string, with the provided data, starting the template engine and templating the provided template, returning the rendered result.
func (*Engine) RunTemplateStringInputWithContext ¶ added in v0.9.0
func (e *Engine) RunTemplateStringInputWithContext(ctx context.Context, name, template string, data any) (string, error)
RunTemplateStringInputWithContext runs the provided input template string, with the provided data, starting the template engine and templating the provided template, returning the rendered result.
func (*Engine) RunTemplateStringWithContext ¶ added in v0.9.0
func (e *Engine) RunTemplateStringWithContext(ctx context.Context, templateFile string, data any) (string, error)
RunTemplateStringWithContext runs the provided template file, with the provided data, starting the template engine and templating the provided template, returning the rendered result.
func (*Engine) RunTemplateWithContext ¶ added in v0.9.0
func (e *Engine) RunTemplateWithContext(ctx context.Context, templateFile string, outFile string, data any) error
RunTemplateWithContext runs the provided template file, with the provided data, starting the template engine and templating the provided template to a file.
type Opt ¶
type Opt func(*Engine)
Opt is a function that configures the Engine.
func WithDebug ¶ added in v0.8.2
func WithDebug() Opt
WithDebug enables debug mode for the engine, which will log additional information when errors occur.
func WithJSFiles ¶ added in v0.4.0
WithJSFiles allows for providing additional javascript files to be loaded into the engine.
func WithJSFuncs ¶
func WithJSFuncs(funcs map[string]func(call CallContext) goja.Value) Opt
WithJSFuncs allows for providing additional functions available to javascript in the engine.
func WithReadFileSystem ¶
WithReadFileSystem sets the file system to use for reading files. This is useful for embedded files or reading from locations other than disk.
func WithSearchLocations ¶ added in v0.5.0
WithSearchLocations allows for providing additional locations to search for templates and scripts.
func WithTemplateFuncs ¶
WithTemplateFuncs allows for providing additional template functions to the engine, available to all templates.
func WithTracer ¶ added in v0.9.0
WithTracer attaches an OpenTelemetry tracer to the engine and enables tracing support.
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
template
Package template contains methods that will template go text/templates files that may contain sjs snippets.
|
Package template contains methods that will template go text/templates files that may contain sjs snippets. |
template/mocks
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
utils
Package utils contains utility functions.
|
Package utils contains utility functions. |
vm
Package vm provides a wrapper around the goja runtime.
|
Package vm provides a wrapper around the goja runtime. |
pkg
|
|
underscore
Package underscore provides the underscore-min.js file.
|
Package underscore provides the underscore-min.js file. |