Skip to content
Jennifer Programming Language

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 extend
  • name {string} - the template name
  • src {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 set
  • entry {string} - the name of the template to render
  • data {json.Value} - the root data node

Returns {string} - the rendered output

Throws

  • {Error} - kind "tengine" if entry (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.

FieldTypeDescription
nameslist of stringthe template names
sourceslist of stringthe template sources, positionally paired with names