Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · template

Docs

std/template

On this page

Index

Types

TemplateErr

struct TemplateErr {
  msg: Str
}

A parse or execution error, with Go’s text (template: t:1: function "nope" not defined).

Source: std/template.xo:9

TemplateErr.display

fn (e: TemplateErr) display() -> Str

The message, Go’s text (decision 0358).

Source: std/template.xo:14

TemplateFn

type TemplateFn = fn(List[json.Value]) -> json.Value ! Str

A template function (FuncMap, decision 0408): its arguments as the executor’s values, its result a value the template prints or passes on (escaped by the html mode’s context); Err text is the call’s error, error calling NAME: text. Pure: a function with effects cannot be called from a render.

Source: std/template.xo:21

Template

struct Template {
  name: Str
  sources: List[(Str, Str)]
  left: Str = "{{"
  right: Str = "}}"
  missing_key_error: Bool = false
  funcs: Map[Str, TemplateFn] = {}
  html: Bool = false
  parsed: Parsed
}

A set of named templates (template.Template and its associated templates): each name’s source, in the order added, the action delimiters, and whether a missing object key is an error (missingkey=error) rather than <no value>.

Source: std/template.xo:27

Template.add

fn (t: Template) add(name: Str, src: Str) -> Template ! TemplateErr

Another named template in the set (t.New(name).Parse(src)).

Source: std/template.xo:59

Template.missing_key

fn (t: Template) missing_key(error: Bool) -> Template

The set with Option(“missingkey=error”) (true) or the default.

Source: std/template.xo:61

Template.render

fn (t: Template) render(data: json.Value) -> Str ! TemplateErr

Execute: the output of the template named t.name with data.

Source: std/template.xo:63

Template.render_named

fn (t: Template) render_named(name: Str, data: json.Value) -> Str ! TemplateErr

ExecuteTemplate: the output of the template name of the set.

Source: std/template.xo:65

Parsed

struct Parsed {}

The parsed trees of a Template (opaque).

Source: std/template.xo:47

Functions

parse

fn parse(name: Str, src: Str, left: Str = "{{", right: Str = "}}", funcs: Map[Str, TemplateFn] = {}) -> Template ! TemplateErr

template.New(name).Delims(left, right).Parse(src): the template, or Go’s parse error. {{define}} blocks become templates of the set.

Source: std/template.xo:51

parse_html

fn parse_html(name: Str, src: Str, left: Str = "{{", right: Str = "}}", funcs: Map[Str, TemplateFn] = {}) -> Template ! TemplateErr

html/template’s New(name).Parse(src): parse with contextual autoescaping (decision 0408). The escaping errors (a template that ends inside a tag, branches ending in different contexts, an action in an ambiguous URL part, a predefined escaper in a pipeline) are render’s, as Go reports them on Execute.

Source: std/template.xo:57

html

fn html(s: Str) -> json.Value

Trusted content for the html mode (decision 0408; Go’s template.HTML, CSS, JS, JSStr, URL, HTMLAttr, Srcset): a json.Value only these make, passed through unescaped in its own context, escaped as text in any other, and the plain string to every other reader.

Source: std/template.xo:71

css

fn css(s: Str) -> json.Value

Source: std/template.xo:72

js

fn js(s: Str) -> json.Value

Source: std/template.xo:73

jsstr

fn jsstr(s: Str) -> json.Value

Source: std/template.xo:74

url

fn url(s: Str) -> json.Value

Source: std/template.xo:75

attr

fn attr(s: Str) -> json.Value

Source: std/template.xo:76

srcset

fn srcset(s: Str) -> json.Value

Source: std/template.xo:77

↑ index