README

himeno/mopress/template does not have a README file

#
Context

type Context = Map[String, Value]

A named set of template variables, as passed to apply_template / apply_template_strict.

#
Template

type Template = Array[TemplateNode]

A parsed template, as produced by parse_template.

#
TemplateParseError

pub suberror TemplateParseError {
TemplateParseError(String)
} derive(Eq,
Debug
)

Error raised when a template's source cannot be parsed into a valid template AST. The associated String carries a human-readable description of the parse failure.

#
TemplateRenderError

pub suberror TemplateRenderError {
UndefinedVariableError(String)
NonArrayForLoopError(String)
PartialLoadError(String)
PartialParseError(String)
RecursivePartialError(String)
} derive(Eq,
Debug
)

Errors that can occur while rendering a parsed template against a variable context, as raised by apply_template_strict.

#
TemplateNode

pub(all) enum TemplateNode {
Text(String)
Variable(String)
If(String, Array[TemplateNode], Array[TemplateNode])
For(String, Array[TemplateNode])
Partial(String)
} derive(Eq,
Debug
)

A single node in a parsed template's AST.

#
Value

pub(all) enum Value {
String(String)
Number(Double)
Bool(Bool)
Array(Array[Value])
Object(Map[String, Value])
} derive(Eq,
Debug
)

A dynamically-typed value available to templates as part of their rendering context.
impl Show for Value

#
Value::from_json

fn Value::from_json(json : Json) -> Value

Converts a Json value into its corresponding Value representation, e.g. for constructing template variables from JSON data (see Summary::flatten).

#
apply_template

fn apply_template(template : Array[TemplateNode], context : Map[String, Value]) -> String

Renders a parsed template against the given variable context, producing the final output string.

Rendering errors (e.g. an undefined variable reference, or a failed partial) are tolerated on a per-node basis: the offending template node is simply rendered as empty output, and rendering continues for the rest of the template, rather than the whole render failing. Use apply_template_strict if such errors should instead abort rendering entirely.

#
apply_template_strict

fn apply_template_strict(template : Array[TemplateNode], context : Map[String, Value]) -> String raise TemplateRenderError

Like apply_template, but raises TemplateRenderError as soon as any node fails to render — e.g. an undefined variable, a non-array value used in a for loop, or a problem loading/parsing a referenced partial — instead of tolerating the failure by rendering that node as empty output.

#
parse_template

fn[T : Show] parse_template(input : T) -> Array[TemplateNode] raise TemplateParseError

Parses input (any Show-able value, typically a String of raw template source) into a template AST.

Raises TemplateParseError if input is not valid template syntax.

Source Files