Docs menu

Go reads data through reflection. obol maps JavaScript values to the same roles:

JavaScript value Acts as a Go
plain object, Map map
array, typed array slice
class instance struct, with its methods
Set, generator, any iterable iterator: range takes one variable
null nil
undefined, a missing key no value
number, bigint int if a safe integer, else float64
string, boolean string, bool

Keys and fields #

A plain object or Map follows missingkey: {{.nope}} prints <no value> by default. A class instance is a struct, so a field it lacks is an error under every option.

Only a plain object's own properties count, and no value reaches constructor or __proto__, so a template cannot climb into the prototype.

Methods #

A method is a function on the prototype. {{.user.greet "Hi"}} calls it with this bound and "Hi" as its argument:

class User {
  name: string
  constructor(name: string) {
    this.name = name
  }
  greet(word: string) {
    return `${word}, ${this.name}`
  }
}

new Template('t').parse('{{.greet "Hi"}}').execute(new User('Ann'))

That returns Hi, Ann.

A function stored in a property is a value, as in Go. {{.fn 1}} fails with fn is not a method but has arguments; run it with {{call .fn 1}}.

A method or function that throws stops the template with error calling name: message. Functions run synchronously: obol does not await a promise.

Printing #

Values print as Go prints them:

Value Prints
[1, 'x', null] [1 x <nil>]
{ a: 1, b: 'x' } map[a:1 b:x]
null, a missing value <no value>
new User('Ann') {Ann}
1e6 1000000
1234567.5 1.2345675e+06

Use printf for any other format.