obol
View as Markdownobol is Go's text/template for Node.js.
It ports Go's lexer, parser and executor, so a template that runs in Go runs
here and prints the same text.
import { Template } from '@memomori/obol'
const page = new Template('page').parse(`
{{- define "item"}}<li>{{.name}}{{if .done}} ✓{{end}}</li>{{end -}}
<h1>{{printf "%s (%d)" .title (len .items) | html}}</h1>
<ul>{{range .items}}{{template "item" .}}{{else}}<li>Nothing yet</li>{{end}}</ul>
`)
page.execute({
title: 'Groceries',
items: [
{ name: 'Rice', done: true },
{ name: 'Tea', done: false },
],
})
<h1>Groceries (2)</h1>
<ul><li>Rice ✓</li><li>Tea</li></ul>
What it covers #
- actions, pipelines, variables, comments and trim markers
if,with,range,breakandcontinue, each withelsedefine,blockandtemplate, and sets of associated templates- every builtin function,
printfverbs included - custom delimiters and
missingkeyoptions parseFilesandparseGlob- Go's error messages, with line and column
It has no dependencies and runs on Node 22.18 or later.
Why #
A Go service and a JavaScript tool can share templates: a site generator, an email preview, an editor that renders what the server will. obol checks itself against about 800 templates run through real Go. See Conformance.
JavaScript forces a few differences, such as one number type.
License #
MIT.