Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

v0 source pure

Readme View source

gno.land/p/nym-moultest123/template

A small {{ }} template engine for building Markdown in a realm, with the p/moul/md helpers available as template functions.

1r := template.NewRenderer()
2
3out := r.Render("{{H1 title}}\n\n{{Bold name}} has {{count}} items", map[string]interface{}{
4	"title": "Report",
5	"name":  "moul",
6	"count": 3,
7})

Syntax

form meaning
{{key}} substitute a value from the data map
{{Func arg}} call a template function
{{range items}} ... {{end}} iterate
{{if cond}} ... {{else}} ... {{end}} conditional
{{- x}}, {{x -}} trim whitespace on that side

Whitespace trimming matters more here than in Go's text/template, because the output is Markdown and a stray blank line changes the rendering.

Built-in functions

Markdown, forwarded to p/moul/md: H1 to H6, Bold, Italic, Strikethrough, InlineCode, CodeBlock, LanguageCodeBlock, Blockquote, Paragraph, BulletItem, TodoItem, HorizontalRule, Link, Image, InlineImageWithLink, Footnote, CollapsibleSection, EscapeText.

General: concat, printf, trim, string, index.

Register your own with the Func signature:

1type Func func(args ...string) string

Why not text/template

It is not in the gno standard library, and it would not fit: it reaches for reflection, and template execution has to stay cheap and bounded when it runs inside a Render. This engine is string in, string out, with an explicit function table and no reflection.

Helper is kept as an alias for backward compatibility; new code should use Func.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

Dependency graph:

gno.land/p/nym-moultest123/template/v0 dependency graph

⚠️ Disclaimer: provided as-is, without warranty; not security-audited. Full disclaimer: DISCLAIMER.

Functions 1

func NewRenderer

1func NewRenderer() *Renderer
source

NewRenderer creates a new template renderer with default functions

Types 3

type Func

func
1type Func func(args ...string) string
source

Func is the signature for template functions

type Helper

interface
1type Helper interface {
2	Execute(args []string, data map[string]interface{}) string
3}
source

Helper is kept for backward compatibility

type Renderer

struct
1type Renderer struct {
2	funcs map[string]Func        // Template functions
3	data  map[string]interface{} // Global data
4	ctx   *context               // Current evaluation context
5}
source

Renderer provides a simple template engine with a clean API

Methods on Renderer

func Render

method on Renderer
1func (r *Renderer) Render(template string, data map[string]interface{}) string
source

Render processes a template with the given data

Dependencies

Source Files 5