liquid

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 12 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

💧 liquid — Liquid templates for Starlark

Go Reference codecov binary footprint

Render Liquid templates from Starlark, built on osteele/liquid (v1.4.0).

Overview

starpkg gives Starlark scripts support for necessary local operations plus simple abstractions over common online services, for ease of use. liquid is a local capability — a pure-Go, offline text-templating primitive with no network or filesystem reach. It depends downward on starpkg/base (the module/config system), 1set/starlet (the Machine + the dataconv value bridge), and transitively 1set/starlight + go.starlark.net; nothing in the ecosystem depends on it.

Liquid is a sandboxed template language: a template can only see the variables you place in its bindings — there is no implicit access to host state or script globals. This module mirrors that model exactly: variables are passed explicitly as a bindings dict and/or keyword arguments.

For the complete per-builtin reference — signatures, parameters, returns, errors, examples — and the configuration accessors, see docs/API.md.

Installation

go get github.com/starpkg/liquid

Quickstart

Wire the module into a Starlet interpreter, then load("liquid", …) from a script:

package main

import (
	"fmt"

	"github.com/1set/starlet"
	"github.com/starpkg/liquid"
)

func main() {
	mod := liquid.NewModule()
	interpreter := starlet.NewWithLoaders(nil, nil, starlet.ModuleLoaderMap{
		"liquid": mod.LoadModule(),
	})

	script := `
load("liquid", "render")
out = render("Hello {{ name }}!", {"name": "World"})
print(out)
`
	if _, err := interpreter.RunScript([]byte(script), nil); err != nil {
		fmt.Printf("Error: %v\n", err)
	}
}

From Starlark, render in one call, or compile once and render many times:

load("liquid", "render", "parse")

# One-shot: bindings dict plus keyword arguments (kwargs win on a name conflict)
render("Hello {{ name }}! You have {{ count }} messages.",
       {"name": "World", "count": 3})
# => "Hello World! You have 3 messages."

# Compile once, render repeatedly
tmpl = parse("{% for x in xs %}{{ x }}{% endfor %}")
tmpl.render({"xs": [1, 2, 3]})        # => "123"

Starlark API at a glance

Top-level builtins (load("liquid", …)):

  • render(source, bindings=None, **kwargs) — parse and render a template in one call; returns the rendered string.
  • parse(source) — compile a template once; returns a Template object for repeated rendering.

Template object (returned by parse):

  • Template.render(bindings=None, **kwargs) — render the compiled template with the given bindings (same merge rule as render).

Both render entry points support {% for %} / {% if %} / {% unless %} / {% case %} control flow and the 48 standard Shopify filters (upcase, join, default, sort, …); {% include %} is disabled. See docs/API.md for full signatures, the filter set, the exact error-message shapes, and examples of every builtin and method above.

Configuration

The module's options (max_output_size, strict, max_render_time) are configured via environment variables (LIQUID_MAX_OUTPUT_SIZE / LIQUID_STRICT / LIQUID_MAX_RENDER_TIME) or per-option accessor builtins, and bound how every subsequent render / parse call behaves. Rendered output is capped at max_output_size bytes (256 KiB by default); strict turns an undefined variable into an error; max_render_time bounds a single render's wall-clock time (0 = no module limit, still bounded by the thread context deadline if the host set one). max_render_time is host-only (no set_max_render_time) so a script cannot disable the DoS time limit.

Note: a render that exceeds the time limit returns an error to the script, but because the underlying engine has no cancellation support the abandoned render finishes on its own (an output-producing runaway stops almost immediately at the output cap; a pathological no-output loop keeps a goroutine busy until it completes). A hard CPU bound requires an OS/sandbox limit at the host layer.

See the Configuration section of docs/API.md for the full option table, defaults, accessors, and safety details.

License

This project is licensed under the MIT License — see the LICENSE file for details.

Documentation

Overview

Package liquid provides a Starlark module for rendering Liquid templates.

Liquid is a sandboxed template language: a template can only see the variables placed in its bindings map — there is no implicit access to the host or to script globals. This module mirrors that model: variables are passed explicitly as a bindings dict (and/or keyword arguments) from the script, converted to the engine's Bindings (map[string]interface{}).

Safety: rendered output is capped (ADR-010), a single render is bounded by a wall-clock deadline (the thread context plus the host-only max_render_time), render panics are recovered into errors, and the filesystem {% include %} tag is disabled so a template cannot read host files.

Index

Constants

View Source
const ModuleName = "liquid"

ModuleName is the name used in Starlark's load() for this module.

Variables

This section is empty.

Functions

This section is empty.

Types

type Module

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

Module wraps a ConfigurableModule with Liquid-specific functions.

func NewModule

func NewModule() *Module

NewModule creates a new Module with default configuration.

func (*Module) LoadModule

func (m *Module) LoadModule() starlet.ModuleLoader

LoadModule returns the Starlark module loader.

Jump to

Keyboard shortcuts

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