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) Close() error
- func (e *Engine) Init(ctx context.Context, data any) error
- func (e *Engine) RunFunction(ctx context.Context, fnName string, args ...any) (goja.Value, error)
- func (e *Engine) RunScript(ctx context.Context, scriptFile string) error
- func (e *Engine) Runtime() *goja.Runtime
- func (e *Engine) TemplateFile(ctx context.Context, templateFile string, outFile string, data any) error
- func (e *Engine) TemplateString(ctx context.Context, templateFilePath string, data any) (string, error)
- func (e *Engine) TemplateStringInput(ctx context.Context, name, template string, data any) (string, error)
- type Opt
- func WithDebug() Opt
- func WithDebugger(port int) Opt
- func WithJSFiles(files map[string]string) Opt
- func WithJSFuncs(funcs map[string]func(call CallContext) goja.Value) Opt
- func WithRandSource(randSource func() float64) 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
- type PanicError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlreadyInitialized is returned when the engine has already been initialized. ErrAlreadyInitialized = errors.New("engine has already been initialized") // ErrNotInitialized is returned when the engine has not been initialized. ErrNotInitialized = errors.New("engine has not been initialized") // 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") // ErrNativePanic is returned when a Go native function panics with a non-goja error. ErrNativePanic = errors.New("native function panic") )
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) Close ¶ added in v0.12.0
Close ends the debug session (if active) and releases resources. For non-debug engines this is a no-op. Safe to call multiple times.
func (*Engine) Init ¶ added in v0.10.0
Init initializes the engine with global data available to all following methods, and should be called before any other methods are called but only once. When using any of the Run or Template methods after init, they will share the global data, but just be careful they will also share any changes made to the environment by previous runs.
If debugging is enabled (via WithDebugger), Init starts a DAP debug server and blocks until a client (e.g., VS Code) connects and sets breakpoints. After Init returns, all RunScript/TemplateFile/etc. calls will hit breakpoints normally. Call Close() when done to end the debug session.
func (*Engine) RunFunction ¶ added in v0.10.0
RunFunction will run the named function if it already exists within the environment, for example if it was defined in a script run by RunScript. The provided args will be passed to the function, and the result will be returned.
func (*Engine) RunScript ¶
RunScript runs the provided script file within the environment initialized by Init. This is useful for setting up the environment with global variables and functions, or running code that is not directly related to templating but might setup the environment for templating.
func (*Engine) Runtime ¶ added in v0.12.0
Runtime returns the underlying goja Runtime, or nil if the engine has not been initialized. This can be used to set up debugging or other advanced runtime configuration.
func (*Engine) TemplateFile ¶ added in v0.10.0
func (e *Engine) TemplateFile(ctx context.Context, templateFile string, outFile string, data any) error
TemplateFile runs the provided template file, with the provided data and writes the result to the provided outFile.
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 WithDebugger ¶ added in v0.12.0
WithDebugger enables DAP (Debug Adapter Protocol) debugging on the specified TCP port. When set, Init() will start a debug server and block until a DAP client (e.g., VS Code) connects. After Init returns, all RunScript, TemplateFile, etc. calls will hit breakpoints. Call Close() when done.
Example:
e := easytemplate.New(easytemplate.WithDebugger(4711)) defer e.Close() e.Init(ctx, data) // blocks until VS Code attaches e.RunScript(ctx, "main.js") // breakpoints work
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 WithRandSource ¶ added in v0.11.2
WithRandSource sets the random source to use 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.
type PanicError ¶ added in v0.12.2
type PanicError struct {
// Cause is the underlying panic value converted to an error.
Cause error
// GoStack is the raw Go stack trace captured at the panic site.
GoStack string
// JSStack is the JS call stack captured from the goja VM at panic time.
JSStack []goja.StackFrame
}
PanicError wraps a Go runtime panic with both the Go stack trace (from the panic site) and the JS call stack (from the goja VM). Downstream consumers can use errors.As to extract the full context.
func (*PanicError) Error ¶ added in v0.12.2
func (e *PanicError) Error() string
func (*PanicError) Unwrap ¶ added in v0.12.2
func (e *PanicError) Unwrap() error
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. |