tengine API reference
A small text template engine for lightweight CMS-style rendering - a subset of Go's text/template (the Go / Hugo style), evaluated directly over a json.Value data tree. There is no compile step and no AST: the engine re-scans block bodies as it renders, which is fine at page / CMS scale. Dotted paths resolve as JSON Pointers against the current node (.a.b -> /a/b; . is the current node); $ is the root node, $var a variable; a missing key renders as empty.
SECURITY: output is not auto-escaped (this is text/template, not html/template). Rendering untrusted data into an HTML page is an XSS hole unless you escape it yourself: pipe every value that reaches HTML through the html pipe ({{ .userInput | html }}), or escape upstream. The engine will emit whatever the data contains, <script> included.
Actions: value output; if / else if / else with the comparison / boolean functions eq ne lt le gt ge and or not (parenthesised nesting allowed); range (with an optional $i, $e := index / element bind) and with, each with an else; variable assignment {{ $x := PIPE }}; {{ define }} / {{ template }} / {{ block }} layout inheritance; {{/* comments */}}; {{- / -}} whitespace-trim markers; and output pipes upper lower title trim html urlize default truncate join len. Over the compiled-in json / strings / lists / maps / convert libraries, so it runs on both binaries.
Import with import "tengine.j" as tengine;. See the tengine guide for prose and examples.
Functions
tengine.add(set as Set, name as string, src as string)
Add a template source under name, returning a new Set. Any top-level {{ define "x" }}...{{ end }} blocks are pulled out and registered as their own entries x, so a page can define the sections its base layout pulls in. Whitespace-trim markers ({{- / -}}) are applied as the source is stored.
Parameters
set{Set}- the set to extendname{string}- the template namesrc{string}- the template source text
Returns {Set} - a new Set with the template (and any defines) added
Throws
{Error}- kind "tengine" on an unterminated action
tengine.newSet()
Create an empty template set.
Returns {Set} - a fresh, empty set
tengine.render(set as Set, entry as string, data as json.Value)
Render the named template against a json.Value data tree.
Parameters
set{Set}- the template setentry{string}- the name of the template to renderdata{json.Value}- the root data node
Returns {string} - the rendered output
Throws
{Error}- kind "tengine" ifentry(or a referenced template) is absent
Structs
tengine.Set
A value-semantic set of named template sources (kept as two parallel lists). Build it with newSet, populate it with add, and render an entry with render.
| Field | Type | Description |
|---|---|---|
names | list of string | the template names |
sources | list of string | the template sources, positionally paired with names |