Recommended: use the Nunchucks CLI as your primary integration path.
npm install @samuelbines/nunchucks
npm link @samuelbines/nunchucks
Nunchucks template
{% extends "layout.njk" %}
{% block body %}
<h2>{{ user.name | title }}</h2>
{% if user.active %}
<p>Status: active</p>
{% endif %}
{% endblock %}
Install CLI (Recommended)
Most users should use the CLI first, then add language wrappers only where needed.
# Build local CLI binary
cd go
go build -o ../bin/nunchucks ./cmd/nunchucks
# Run
../bin/nunchucks help
../bin/nunchucks render -views ./views -template index.njk -data '{"title":"Hello"}'
# Or install with Go
go install github.com/SamuelDBines/nunjucks/go/cmd/nunchucks@latest
# CLI
go run ./cmd/nunchucks help
go run ./cmd/nunchucks render -views ./views -template index.njk -data '{"title":"Hello"}'
go run ./cmd/nunchucks precompile -views ./views -out ./public -data '{"title":"Hello"}'
# WASM build
cd go
GOOS=js GOARCH=wasm go build -o wasm/nunchucks.wasm ./cmd/nunchucks-wasm
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" wasm/wasm_exec.js
Examples: go run ./examples/basic and
go run ./examples/advanced and
go run ./examples/client.
go run ./cmd/nunchucks help
go run ./cmd/nunchucks render -views ./views -template index.njk -data '{"title":"Hello"}'
go run ./cmd/nunchucks precompile -views ./views -out ./public -data '{"title":"Hello"}'
Examples
cd go
go run ./examples/basic
go run ./examples/advanced
Detailed Example A: Template Composition
# views/layout.njk
<h1>{% block title %}Title{% endblock %}</h1>
<main>{% block body %}{% endblock %}</main>
# views/macros.njk
{% macro badge(text) %}<span class="badge">{{ text | upper }}</span>{% endmacro %}
# views/home.njk
{% extends "layout.njk" %}
{% import "macros.njk" as ui %}
{% block title %}Users{% endblock %}
{% block body %}
{% for u in users %}
{{ ui.badge(u.name) }}
{% endfor %}
{% endblock %}
The Go core can be compiled to WebAssembly and consumed from JS
runtimes.
cd go
GOOS=js GOARCH=wasm go build -o wasm/nunchucks.wasm ./cmd/nunchucks-wasm
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" wasm/wasm_exec.js
Then load with go/wasm/loader.js and call:
renderFromMap({ template, files, context })
renderString({ source, context })
Client Runtime
Nunchucks now supports a first client-runtime layer in the Go engine.
{% client %}...{% endclient %} compiles to browser module
scripts, {% fetch %} compiles to async fetch calls, and
{% state %} bootstraps state under
window.__nunchucks.state.
The local app builder now stores files in browser IndexedDB rather than
writing to shared server files by default. This makes per-user sessions
isolated and safer for multi-user usage.
Controls available in the app builder: Reset,
Import, Export, and
Download output.
Tags
Current Go runtime support:
if, for, set
extends, block, include
macro, import ... as, call
raw, verbatim,
filter ... endfilter
{% import "macros.njk" as ui %}
{% set name = "sam" %}
{% if name %}{{ ui.badge(name) }}{% endif %}