scriggo

package module
v0.0.0-...-c2c195c Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: BSD-3-Clause Imports: 18 Imported by: 1

README

Scriggo

The world’s most powerful template engine and Go embeddable interpreter.

Go Reference Go Report Card

Website | Get Started | Documentation | Community | Contributing

Features

  • Fast, a very fast embeddable pure Go language interpreter.
  • Modern and powerful template engine with Go as scripting language.
  • Native support for Markdown in templates.
  • Secure by default. No access to packages unless explicitly enabled.
  • Easy to embed and to interop with any Go application.

Get Started with Programs

Execute a Go program embedded in your application:

package main

import "gitlab.com/haproxy-haptic/scriggo"

func main() {

    // src is the source code of the program to run.
    src := []byte(`
        package main

        func main() {
            println("Hello, World!")
        }
    `)

    // Create a file system with the file of the program to run.
    fsys := scriggo.Files{"main.go": src}

    // Build the program.
    program, err := scriggo.Build(fsys, nil)
    if err != nil {
        panic(err)
    }
 
    // Run the program.
    err = program.Run(nil)
    if err != nil {
        panic(err)
    }

}

Get Started with Templates

Scriggo, in templates, supports inheritance, macros, partials, imports and contextual autoescaping but most of all it uses the Go language as the template scripting language.

{% extends "layout.html" %}
{% import "banners.html" %}
{% macro Body %}
    <ul>
      {% for product in products %}
      <li><a href="{{ product.URL }}">{{ product.Name }}</a></li>
      {% end %}
    </ul>
    {{ render "pagination.html" }}
    {{ Banner() }}
{% end %}

Scriggo template files can be written in plain text, HTML, Markdown, CSS, JavaScript and JSON.

Execute a Scriggo template in your application

// Build and run a Scriggo template.
package main

import (
	"os"

	"gitlab.com/haproxy-haptic/scriggo"
	"gitlab.com/haproxy-haptic/scriggo/builtin"
	"gitlab.com/haproxy-haptic/scriggo/native"
)

func main() {

    // Content of the template file to run.
    content := []byte(`
    <!DOCTYPE html>
    <html>
    <head>Hello</head> 
    <body>
        Hello, {{ capitalize(who) }}!
    </body>
    </html>
    `)

    // Create a file system with the file of the template to run.
    fsys := scriggo.Files{"index.html": content}

    // Declare some globals.
    var who = "world"
    opts := &scriggo.BuildOptions{
        Globals: native.Declarations{
            "who":        &who,               // global variable
            "capitalize": builtin.Capitalize, // global function
        },
    }

    // Build the template.
    template, err := scriggo.BuildTemplate(fsys, "index.html", opts)
    if err != nil {
        panic(err)
    }
 
    // Run the template and print it to the standard output.
    err = template.Run(os.Stdout, nil, nil)
    if err != nil {
        panic(err)
    }

}

For a complete get started guide see the Scriggo site.

Contributing

Want to help contribute to Scriggo? See CONTRIBUTING.md.

Documentation

Index

Examples

Constants

View Source
const (
	MutationSetVariable    = runtime.MutationSetVariable
	MutationSetPointer     = runtime.MutationSetPointer
	MutationSetField       = runtime.MutationSetField
	MutationSetMapIndex    = runtime.MutationSetMapIndex
	MutationSetIndex       = runtime.MutationSetIndex
	MutationDelete         = runtime.MutationDelete
	MutationClear          = runtime.MutationClear
	MutationAppend         = runtime.MutationAppend
	MutationCopy           = runtime.MutationCopy
	MutationCloseChannel   = runtime.MutationCloseChannel
	MutationSendChannel    = runtime.MutationSendChannel
	MutationReceiveChannel = runtime.MutationReceiveChannel
	MutationSelectChannel  = runtime.MutationSelectChannel
)

Variables

View Source
var ErrBatchDetachedWork = runtime.ErrBatchDetachedWork

ErrBatchDetachedWork is returned before batch execution starts when the program contains a goroutine or parallel render operation.

View Source
var ErrBatchUncertifiedNative = runtime.ErrBatchUncertifiedNative

ErrBatchUncertifiedNative is returned before batch execution starts when a referenced native capability lacks a synchronous declaration certificate.

View Source
var ErrDeterministicGo = runtime.ErrDeterministicGo

ErrDeterministicGo is returned before deterministic execution starts when the program contains a goroutine or parallel render operation.

View Source
var ErrDeterministicMapKey = runtime.ErrDeterministicMapKey

ErrDeterministicMapKey is returned when deterministic mode encounters a map key whose identity has no stable, value-based order.

View Source
var ErrDeterministicPrint = runtime.ErrDeterministicPrint

ErrDeterministicPrint is returned before execution when deterministic mode finds a language-level print or println call.

View Source
var ErrMutationObserverChannelOperation = runtime.ErrMutationObserverChannelOperation

ErrMutationObserverChannelOperation is returned before a channel close, send, receive, or select while ObserveMutation is set.

View Source
var ErrMutationObserverDetachedGo = runtime.ErrMutationObserverDetachedGo

ErrMutationObserverDetachedGo is returned before a detached Scriggo goroutine starts while ObserveMutation is set. Waited go-render operations remain available.

View Source
var ErrVectorGenerationRevoked = runtime.ErrVectorGenerationRevoked

ErrVectorGenerationRevoked reports a callable invoked outside its vector item.

View Source
var ErrVectorInactive = runtime.ErrVectorInactive

ErrVectorInactive reports an activation lifecycle violation.

View Source
var ErrVectorInvalidConfiguration = runtime.ErrVectorInvalidConfiguration

ErrVectorInvalidConfiguration reports a malformed or unauthenticated vector run.

Functions

func ClearVMPool

func ClearVMPool()

ClearVMPool releases pooled VMs to allow garbage collection. Call this after large rendering operations complete to reduce memory usage from parallel rendering spikes (go render statements).

The pool will rebuild naturally on the next render operation - there is no persistent impact on performance, just a one-time ~10ms overhead for creating new VMs on the first render after clearing.

Thread-safe: can be called from any goroutine.

func HTMLEscape

func HTMLEscape(s string) native.HTML

HTMLEscape escapes s, replacing the characters <, >, &, " and ' and returns the escaped string as HTML type.

Use HTMLEscape to put a trusted or untrusted string into an HTML element content or in a quoted attribute value. But don't use it with complex attributes like href, src, style, or any of the event handlers like onmouseover.

Example
package main

import (
	"fmt"

	"gitlab.com/haproxy-haptic/scriggo"
)

func main() {
	fmt.Println(scriggo.HTMLEscape("Rock & Roll!"))
}
Output:
Rock &amp; Roll!

func NativeFunctionUsesReflection

func NativeFunctionUsesReflection(typ reflect.Type) bool

NativeFunctionUsesReflection reports whether calls of typ use the reflective fallback.

func ResetNativeFrameCallStatistics

func ResetNativeFrameCallStatistics()

ResetNativeFrameCallStatistics resets compiled frame dispatch counters.

Types

type BatchRun

type BatchRun struct {
	Out     io.Writer
	Vars    map[string]any
	Options *RunOptions
	Before  func() error
	After   func()
}

BatchRun describes one isolated execution in Template.RunBatch.

type BatchRunError

type BatchRunError struct {
	Index int
	Err   error
}

BatchRunError reports the first failed execution in Template.RunBatch.

func (*BatchRunError) Error

func (e *BatchRunError) Error() string

func (*BatchRunError) Unwrap

func (e *BatchRunError) Unwrap() error

type BuildError

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

BuildError represents an error occurred building a program or template.

Example
package main

import (
	"fmt"

	"gitlab.com/haproxy-haptic/scriggo"
)

func main() {
	fsys := scriggo.Files{
		"index.html": []byte(`{{ 42 + true }}`),
	}
	_, err := scriggo.BuildTemplate(fsys, "index.html", nil)
	if err != nil {
		fmt.Printf("Error has type %T\n", err)
		fmt.Printf("Error message is: %s\n", err.(*scriggo.BuildError).Message())
		fmt.Printf("Error path is: %s\n", err.(*scriggo.BuildError).Path())
	}
}
Output:
Error has type *scriggo.BuildError
Error message is: invalid operation: 42 + true (mismatched types int and bool)
Error path is: index.html

func (*BuildError) Error

func (err *BuildError) Error() string

Error returns a string representation of the error.

func (*BuildError) Message

func (err *BuildError) Message() string

Message returns the error message.

func (*BuildError) Path

func (err *BuildError) Path() string

Path returns the path of the file where the error occurred.

func (*BuildError) Position

func (err *BuildError) Position() Position

Position returns the position in the file where the error occurred.

type BuildOptions

type BuildOptions struct {

	// AllowGoStmt, when true, allows the use of the go statement.
	AllowGoStmt bool

	// Packages is a package importer that makes native packages available
	// in programs and templates through the import statement.
	Packages native.Importer

	// UnexpandedTransformer transforms an unexpanded AST.
	// If non-nil, it is invoked on each parsed file before expansion.
	//
	// Used for templates only.
	UnexpandedTransformer func(tree *ast.Tree) error

	// ExpandedTransformer transforms the expanded AST.
	// If non-nil, it is invoked on the expanded tree before type checking.
	//
	// Used for templates only.
	ExpandedTransformer func(tree *ast.Tree) error

	// NoParseShortShowStmt, when true, don't parse the short show statements.
	//
	// Used for templates only.
	NoParseShortShowStmt bool

	// MarkdownConverter converts a Markdown source code to HTML.
	//
	// Used for templates only.
	MarkdownConverter Converter

	// Globals declares constants, types, variables, functions and packages
	// that are accessible from the code in the template.
	//
	// Used for templates only.
	Globals native.Declarations

	// EnableProfiling enables profiling instrumentation in the compiled template.
	// When true, the runtime will collect timing data for function calls,
	// macros, and includes during execution.
	//
	// Used for templates only.
	EnableProfiling bool

	// IsolateRootRenderState makes each render expression in the root template
	// reinitialize package variables reachable through the rendered file.
	// Nested renders share the state of their enclosing root render.
	//
	// Used for templates only.
	IsolateRootRenderState bool
}

BuildOptions contains options for building programs and templates.

type CallKind

type CallKind int

CallKind distinguishes different types of calls in profiling. Internal implementation: runtime.ProfileCallKind (internal/runtime/profiler.go)

const (
	// CallKindScriggoFunc is a call to a Scriggo-defined function.
	CallKindScriggoFunc CallKind = iota
	// CallKindNativeFunc is a call to a native Go function.
	CallKindNativeFunc
	// CallKindMacro is a macro invocation.
	CallKindMacro
)

func (CallKind) String

func (k CallKind) String() string

String returns a string representation of the CallKind.

type CallNode

type CallNode struct {
	// Call is the call record for this node (nil for the root node).
	Call *CallRecord
	// Children are the child calls.
	Children []*CallNode
}

CallNode represents a node in the call tree.

type CallRecord

type CallRecord struct {
	// ID is a unique identifier for this call.
	ID uint64
	// ParentID links to the parent call (0 for root calls).
	ParentID uint64

	// Name is the function or macro name.
	Name string
	// Package is the package path (empty for macros).
	Package string
	// File is the source file path.
	File string
	// Line is the source line number.
	Line int

	// Kind indicates the type of call.
	Kind CallKind

	// StartTime is when the call began.
	StartTime time.Time
	// Duration is how long the call took.
	Duration time.Duration
	// SelfDuration excludes time spent in child calls.
	SelfDuration time.Duration

	// TemplatePath is the template path for include calls.
	TemplatePath string
}

CallRecord represents a single function or macro invocation during profiling. Internal implementation: runtime.ProfileCallRecord (internal/runtime/profiler.go)

type Converter

type Converter func(src []byte, out io.Writer) error

Converter is implemented by format converters.

type ExitError

type ExitError struct {
	Code int
	Err  error
}

ExitError represents an exit from an execution with a non-zero status code. It may wrap the error that caused the exit.

An ExitError is conventionally passed to the Stop method of [native.Env so that the error code can be used as the process exit code.

func NewExitError

func NewExitError(code int, err error) *ExitError

NewExitError returns an exit error with the given status code and error. The status code should be in the range [1, 125] and err can be nil. It panics if code is zero.

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

type Files

type Files map[string][]byte

Files implements a file system that read the files from a map.

func (Files) Open

func (fsys Files) Open(name string) (fs.File, error)

Open opens the named file.

type Format

type Format int

A Format represents a content format.

const (
	FormatText Format = iota
	FormatHTML
	FormatCSS
	FormatJS
	FormatJSON
	FormatMarkdown
)

Formats.

func (Format) String

func (format Format) String() string

String returns the name of the format.

type FormatFS

type FormatFS interface {
	fs.FS
	Format(name string) (Format, error)
}

FormatFS is the interface implemented by a file system that can determine the file format from a path name.

type FunctionStats

type FunctionStats struct {
	// Name is the function or macro name.
	Name string
	// Package is the package path (empty for macros).
	Package string
	// CallCount is the number of times this function was called.
	CallCount int
	// TotalTime is the sum of all call durations.
	TotalTime time.Duration
	// SelfTime is the sum of all self durations.
	SelfTime time.Duration
	// MinTime is the minimum call duration.
	MinTime time.Duration
	// MaxTime is the maximum call duration.
	MaxTime time.Duration
}

FunctionStats contains per-function statistics.

type Mutation

type Mutation = runtime.Mutation

Mutation describes storage immediately before a template mutates it. Target is the variable, pointer, struct, map, slice, array, or channel whose reachable storage will change. An observer must treat Target as read-only.

type MutationOperation

type MutationOperation = runtime.MutationOperation

MutationOperation identifies a template-language mutation.

type NativeCall

type NativeCall = runtime.NativeCall

NativeCall describes a native method immediately before Scriggo calls it or exposes it as a method value. Receiver must be treated as read-only by an observer. MethodValue reports the latter case.

type NativeCallableKind

type NativeCallableKind uint8

NativeCallableKind identifies a native callable surface.

const (
	NativeCallableMethod NativeCallableKind = iota
	NativeCallableFunctionField
	NativeCallableIndexedFunction
	NativeCallableFunctionResult
)

type NativeDeclarationKind

type NativeDeclarationKind uint8

NativeDeclarationKind identifies how a native declaration is bound.

const (
	NativeDeclarationVariable NativeDeclarationKind = iota
	NativeDeclarationFunction
	NativeDeclarationType
	NativeDeclarationConstant
)

type NativeFrameCallStatistics

type NativeFrameCallStatistics = runtime.NativeFrameCallStatistics

NativeFrameCallStatistics reports compiled frame dispatch outcomes.

func ReadNativeFrameCallStatistics

func ReadNativeFrameCallStatistics() NativeFrameCallStatistics

ReadNativeFrameCallStatistics reads compiled frame dispatch counters.

type PanicError

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

PanicError represents the error that occurs when an executed program or template calls the panic built-in and the panic is not recovered.

func (*PanicError) Error

func (p *PanicError) Error() string

Error returns all currently active panics as a string.

To print only the message, use the PanicError.String method instead.

func (*PanicError) Message

func (p *PanicError) Message() any

Message returns the panic message.

func (*PanicError) Next

func (p *PanicError) Next() *PanicError

Next returns the next panic in the chain.

func (*PanicError) Path

func (p *PanicError) Path() string

Path returns the path of the file that panicked.

func (*PanicError) Position

func (p *PanicError) Position() Position

Position returns the position in file where the panic occurred.

func (*PanicError) Recovered

func (p *PanicError) Recovered() bool

Recovered reports whether it has been recovered.

func (*PanicError) StackFrames

func (p *PanicError) StackFrames() []StackFrame

StackFrames returns the call stack as structured frames. The first frame is the immediate location of the panic, subsequent frames show the call chain up to the entry point.

func (*PanicError) StackTrace

func (p *PanicError) StackTrace() string

StackTrace returns the stack trace at the point of the panic. The trace shows the call chain with file paths and line numbers.

func (*PanicError) String

func (p *PanicError) String() string

String returns the panic message as a string.

type Position

type Position struct {
	Line   int // line starting from 1
	Column int // column in characters starting from 1
	Start  int // index of the first byte
	End    int // index of the last byte
}

Position is a position in a file.

func (Position) String

func (p Position) String() string

String returns line and column separated by a colon, for example "37:18".

type PrintFunc

type PrintFunc func(any)

PrintFunc represents a function that prints the arguments of the print and println builtins.

type Profile

type Profile struct {
	// StartTime is when execution began.
	StartTime time.Time
	// EndTime is when execution completed.
	EndTime time.Time
	// TotalDuration is the wall-clock execution time.
	TotalDuration time.Duration

	// Calls contains all recorded function/macro calls in order.
	Calls []*CallRecord
	// contains filtered or unexported fields
}

Profile contains the profiling results from a template execution. Internal implementation: runtime.ProfileData (internal/runtime/profiler.go)

func (*Profile) Stats

func (p *Profile) Stats() *ProfileStats

Stats returns aggregated statistics by function. This method computes the statistics lazily on first call and caches the result.

func (*Profile) Tree

func (p *Profile) Tree() *CallNode

Tree returns the hierarchical call tree. The root node has a nil Call and its Children are the top-level calls. This method builds the tree lazily on first call and caches the result.

type ProfileStats

type ProfileStats struct {
	// TotalCalls is the total number of function/macro calls.
	TotalCalls int
	// NativeCalls is the count of native Go function calls.
	NativeCalls int
	// ScriggoCalls is the count of Scriggo function calls.
	ScriggoCalls int
	// MacroCalls is the count of macro invocations.
	MacroCalls int
	// ByFunction maps function key (pkg.name or name for macros) to stats.
	ByFunction map[string]*FunctionStats
}

ProfileStats contains aggregated profiling statistics.

type Program

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

Program is a program compiled with the Build function.

func Build

func Build(fsys fs.FS, options *BuildOptions) (*Program, error)

Build builds a program from the package in the root of fsys with the given options.

Current limitation: fsys can contain only one Go file in its root.

If a build error occurs, it returns a *BuildError.

Example
package main

import (
	"log"

	"gitlab.com/haproxy-haptic/scriggo"
)

func main() {
	fsys := scriggo.Files{
		"main.go": []byte(`
			package main

			func main() { }
		`),
	}
	_, err := scriggo.Build(fsys, nil)
	if err != nil {
		log.Fatal(err)
	}
}

func (*Program) Disassemble

func (p *Program) Disassemble(pkgPath string) ([]byte, error)

Disassemble disassembles the package with the given path and returns its assembly code. Native packages can not be disassembled.

func (*Program) Run

func (p *Program) Run(options *RunOptions) error

Run starts the program and waits for it to complete. It can be called concurrently by multiple goroutines.

If the executed program panics, and it is not recovered, Run returns a *PanicError.

If the Stop method of native.Env is called, Run returns the argument passed to Stop.

If the Fatal method of native.Env is called, Run panics with the argument passed to Fatal.

If the context has been canceled, Run returns the error returned by the Err method of the context.

Example
package main

import (
	"fmt"
	"log"

	"gitlab.com/haproxy-haptic/scriggo"
	"gitlab.com/haproxy-haptic/scriggo/native"
)

func main() {
	fsys := scriggo.Files{
		"main.go": []byte(`
			package main

			import "fmt"

			func main() {
				fmt.Println("Hello, I'm Scriggo!")
			}
		`),
	}
	opts := &scriggo.BuildOptions{
		Packages: native.Packages{
			"fmt": native.Package{
				Name: "fmt",
				Declarations: native.Declarations{
					"Println": fmt.Println,
				},
			},
		},
	}
	program, err := scriggo.Build(fsys, opts)
	if err != nil {
		log.Fatal(err)
	}
	err = program.Run(nil)
	if err != nil {
		log.Fatal(err)
	}
}
Output:
Hello, I'm Scriggo!

type RunOptions

type RunOptions struct {
	// Deterministic orders map ranges by key and disables print, println,
	// goroutines, and parallel rendering. Functions containing those operations
	// and statically unsupported map key types are rejected before execution.
	// Dynamic pointer, channel, unsafe-pointer, and NaN map keys are rejected when
	// their range begins. Native functions and methods remain outside this policy.
	Deterministic bool

	// Vector installs concrete slice columns into every mutable global slot
	// with the corresponding name. A native.VectorEnv controller selects the
	// active element. Used for templates only.
	Vector *VectorRunOptions

	// NativeFunctionTrampolines provide direct paths for function values created
	// by native.MakeFunctionTrampoline. Other native functions use normal dispatch.
	NativeFunctionTrampolines []*native.FunctionTrampoline

	// Context is a context that can be read by native functions and methods
	// via the Context method of native.Env. Canceling the context, the
	// execution is terminated and the Run method returns Context.Err().
	Context context.Context

	// ChildContext derives the context exposed through native.Env in a child
	// execution from the context of its parent execution. It is called for
	// Scriggo functions invoked by native code and for executions started by a
	// go statement, including parallel template renders. The returned context
	// must derive from parent. Calls may occur concurrently.
	ChildContext func(parent context.Context) context.Context

	// ObserveMutation is called synchronously immediately before template code
	// mutates reachable storage. Returning an error prevents the mutation and
	// makes Run return that error. The Env carries the current child execution
	// context. Calls may occur concurrently.
	//
	// Native functions and methods are outside this observer. Clone or guard
	// mutable values before passing them to native code, or use
	// ObserveNativeCall for method receivers. Detached Scriggo go
	// statements return [ErrMutationObserverDetachedGo] before they start.
	// Channel operations return this function's error, or
	// [ErrMutationObserverChannelOperation] when it returns nil. A select reports
	// every channel candidate; an empty select has an invalid Target.
	ObserveMutation func(env native.Env, mutation Mutation) error

	// ObserveMutationContext is the non-escaping context-only form of ObserveMutation.
	ObserveMutationContext func(ctx context.Context, mutation Mutation) error

	// ObserveNativeCall is called synchronously before Scriggo calls a native
	// method or exposes it as a method value. Returning an error prevents the
	// access and makes Run return that error. It is not called for native
	// functions. Calls may occur concurrently.
	ObserveNativeCall func(env native.Env, call NativeCall) error

	// ObserveNativeCallContext is the non-escaping context-only form of ObserveNativeCall.
	ObserveNativeCallContext func(ctx context.Context, call NativeCall) error

	// BeforeNativeCall is called synchronously immediately before every native
	// function or method invocation. Returning an error prevents the call and
	// makes Run return that error. Calls may occur concurrently.
	BeforeNativeCall func(env native.Env) error

	// BeforeNativeCallContext is the non-escaping context-only form of BeforeNativeCall.
	BeforeNativeCallContext func(ctx context.Context) error

	// Print is called by the print and println builtins to print values.
	// If it is nil, the print and println builtins format their arguments as
	// expected and write the result to standard error.
	Print PrintFunc

	// Profile receives profiling data after execution completes.
	// Only populated if the template was built with EnableProfiling=true.
	// Caller must allocate: &scriggo.Profile{}
	//
	// Note: Native function calls executed as goroutines (via go statement)
	// are not included in the profile since their execution is asynchronous.
	//
	// Used for templates only. Ignored for programs.
	Profile *Profile

	// MaxParallelRenders limits the number of concurrent "go render" operations.
	// If zero or negative, defaults to 2*GOMAXPROCS.
	//
	// Used for templates only. Ignored for programs.
	MaxParallelRenders int

	// CollectSourceMap requests an output-to-source line map: after Run,
	// SourceSpans holds one span per contiguous run of rendered output,
	// attributing it to the template source position that produced it. This
	// is pure observation and does not change the rendered bytes, but adds
	// per-write bookkeeping, so leave it false for production rendering.
	//
	// Used for templates only. Ignored for programs.
	CollectSourceMap bool

	// SourceSpans is populated after Run when CollectSourceMap is true. The
	// spans are in output order and their Length fields sum to the output
	// size, so a caller can walk the rendered bytes and attribute each output
	// line to (Path, Line). Output produced by a parallel "{{ go … }}" render
	// is attributed as one span to the go-render call site.
	SourceSpans []SourceSpan
}

RunOptions are the run options.

type SourceFrame

type SourceFrame struct {
	Path string
	Line int
}

SourceFrame is one level of a template include stack: the template registry key (e.g. "haproxy.cfg" for the main template, or a snippet/map/file name) and the 1-based source line active in it.

type SourceSpan

type SourceSpan struct {
	// Frames is the include stack, innermost first: Frames[0] is where the text
	// literally is, followed by the render/include callers up to the entry
	// template. Piped renders ("{{ render \"x\" | indent }}") resolve into x's
	// own source when the filter is line-preserving.
	Frames []SourceFrame
	// Length is the number of output bytes this span covers.
	Length int
	// IsText reports whether the span is literal template text (so Frames[0].Line
	// advances with each '\n' in the output) rather than a show-expression value.
	IsText bool
}

SourceSpan attributes a contiguous run of rendered output to the template include stack that produced it. See RunOptions.CollectSourceMap.

type StackFrame

type StackFrame struct {
	Function string // Function name
	Path     string // File path
	Line     int    // Line number
}

StackFrame represents a single frame in the call stack.

type Template

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

Template is a template compiled with the BuildTemplate function.

func BuildTemplate

func BuildTemplate(fsys fs.FS, name string, options *BuildOptions) (*Template, error)

BuildTemplate builds the named template file rooted at the given file system. Imported, rendered and extended files are read from fsys.

If fsys implements FormatFS, file formats are read with its Format method, otherwise it depends on the file name extension

HTML       : .html
CSS        : .css
JavaScript : .js
JSON       : .json
Markdown   : .md .mdx .mkd .mkdn .mdown .markdown
Text       : all other extensions

If the named file does not exist, BuildTemplate returns an error satisfying errors.Is(err, fs.ErrNotExist).

If a build error occurs, it returns a *BuildError.

Example
package main

import (
	"log"

	"gitlab.com/haproxy-haptic/scriggo"
)

func main() {
	fsys := scriggo.Files{
		"index.html": []byte(`{% name := "Scriggo" %}Hello, {{ name }}!`),
	}
	_, err := scriggo.BuildTemplate(fsys, "index.html", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func (*Template) BatchSafe

func (t *Template) BatchSafe() bool

BatchSafe reports whether the compiled template can execute in a batch without launching work that can outlive an individual execution.

func (*Template) DeterministicSafe

func (t *Template) DeterministicSafe() error

DeterministicSafe reports whether deterministic execution can run the compiled template. A non-nil error identifies the rejected operation.

func (*Template) Disassemble

func (t *Template) Disassemble(n int) []byte

Disassemble disassembles a template and returns its assembly code.

n determines the maximum length, in runes, of a disassembled text:

n > 0: at most n runes; leading and trailing white space are removed
n == 0: no text
n < 0: all text

func (*Template) Format

func (t *Template) Format() Format

Format returns the output format of the template when executed.

func (*Template) Run

func (t *Template) Run(out io.Writer, vars map[string]any, options *RunOptions) error

Run runs the template and write the rendered code to out. vars contains the values of the global variables. It can be called concurrently by multiple goroutines.

If the executed template panics, and it is not recovered, Run returns a *PanicError.

If the Stop method of native.Env is called, Run returns the argument passed to Stop.

If the Fatal method of native.Env is called, Run panics with the argument passed to Fatal.

If the context has been canceled, Run returns the error returned by the Err method of the context.

If a call to out.Write returns an error, a panic occurs. If the executed code does not recover the panic, Run returns the error returned by out.Write.

Example
package main

import (
	"log"
	"os"

	"gitlab.com/haproxy-haptic/scriggo"
)

func main() {
	fsys := scriggo.Files{
		"index.html": []byte(`{% name := "Scriggo" %}Hello, {{ name }}!`),
	}
	template, err := scriggo.BuildTemplate(fsys, "index.html", nil)
	if err != nil {
		log.Fatal(err)
	}
	err = template.Run(os.Stdout, nil, nil)
	if err != nil {
		log.Fatal(err)
	}
}
Output:
Hello, Scriggo!

func (*Template) RunBatch

func (t *Template) RunBatch(runs []BatchRun) error

RunBatch runs the same compiled template over independent variables, contexts, options, and writers while reusing the virtual machine. It stops at the first error. Output written by earlier executions is not rolled back.

func (*Template) UsedNativeCallables

func (t *Template) UsedNativeCallables() []UsedNativeCallable

UsedNativeCallables returns every native callable surface used by the compiled template and its nested function graph.

func (*Template) UsedNativeDeclarations

func (t *Template) UsedNativeDeclarations() []UsedNativeDeclaration

UsedNativeDeclarations returns every native function, variable, type, and constant referenced by the compiled template and its nested function graph.

func (*Template) UsedNativeValueAccesses

func (t *Template) UsedNativeValueAccesses() []UsedNativeValueAccess

UsedNativeValueAccesses returns native values consumed outside pure selector or callable-selection chains.

func (*Template) UsedVariables

func (t *Template) UsedVariables() []UsedVariable

UsedVariables returns the global variables referenced by the compiled template. A variable used only in dead code may not be returned.

func (*Template) UsedVars

func (t *Template) UsedVars() []string

UsedVars returns the names of the global variables used in the template. A variable used in dead code may not be returned as used.

type UsedNativeCallable

type UsedNativeCallable struct {
	Kind            NativeCallableKind
	Receiver        reflect.Type
	Name            string
	Path            string
	Package         string
	DeclarationName string
	Declaration     native.Declaration
	MemberPath      string
	Synchronous     bool
	Constructed     bool
}

UsedNativeCallable describes one selected or invoked native callable. Receiver and Name identify its exact static surface.

type UsedNativeDeclaration

type UsedNativeDeclaration struct {
	Package            string
	Name               string
	Kind               NativeDeclarationKind
	Declaration        native.Declaration
	Synchronous        bool
	SynchronousMembers []string
}

UsedNativeDeclaration describes one native declaration referenced by a compiled template. Declaration is the exact value supplied at build time.

type UsedNativeValueAccess

type UsedNativeValueAccess struct {
	Package         string
	DeclarationName string
	Declaration     native.Declaration
	MemberPath      string
}

UsedNativeValueAccess describes a native value consumed outside a pure selector or callable-selection chain. An empty MemberPath identifies the declaration's root value.

type UsedVariable

type UsedVariable struct {
	Package          string
	Path             string
	Name             string
	Native           bool
	FunctionPath     string
	FunctionName     string
	FunctionPosition ast.Position
}

UsedVariable identifies one global variable referenced by compiled code. Path is empty for a native declaration and identifies the declaring template file for a Scriggo package variable.

type VectorRunOptions

type VectorRunOptions struct {
	Authority        *native.VectorAuthority
	Count            int
	Bindings         map[string]any
	Contexts         []context.Context
	DeferredBindings []string
	VMNative         bool
	Boundary         *native.VectorBoundary
	Lifecycle        native.VectorLifecycle
	FiberBoundary    *native.VectorFiberBoundary
	FiberLifecycle   native.VectorFiberLifecycle
}

VectorRunOptions configure columnar globals for one template execution.

Directories

Path Synopsis
ast
Package ast declares the types used to define program and template trees.
Package ast declares the types used to define program and template trees.
astutil
Package astutil implements methods to walk and dump a tree.
Package astutil implements methods to walk and dump a tree.
Package builtin provides simple functions, types, constants and a package that can be used as globals in a Scriggo template.
Package builtin provides simple functions, types, constants and a package that can be used as globals in a Scriggo template.
cmd
scriggo command
internal
compiler
Package compiler implements parsing, type checking and emitting of sources.
Package compiler implements parsing, type checking and emitting of sources.
compiler/types
Package types implements functions and types to represent and work with Scriggo types.
Package types implements functions and types to represent and work with Scriggo types.
Package native provides types to implement native variables, constants, functions, types and packages that can be imported or used as builtins in programs and templates.
Package native provides types to implement native variables, constants, functions, types and packages that can be imported or used as builtins in programs and templates.

Jump to

Keyboard shortcuts

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