Sablon.Expression (sablon v0.4.4)

Copy Markdown View Source

The tiny expression language used inside merge fields.

An expression is either a plain context lookup («=title») or a chain of lookups separated by dots («=buyer.address.street»). As in the Ruby gem the dot operator always performs a lookup rather than a function call, so hashes, keyword lists and structs all behave the same way.

Summary

Functions

Evaluates an expression against a context.

Looks a single key up in value.

Parses an expression string.

Applies a conditional predicate such as present? or nil? to value.

Renders an expression the way sablon reports it in errors, e.g. «post.title».

Types

Functions

evaluate(arg1, context)

@spec evaluate(t(), map()) :: term()

Evaluates an expression against a context.

lookup(struct, key)

@spec lookup(term(), binary()) :: term()

Looks a single key up in value.

Maps are searched by string key first and then by atom key, keyword lists by atom key, and structs by field name. Lists answer size, length, count, first, last and a numeric index; strings answer size and length.

parse(expression)

@spec parse(binary()) :: t()

Parses an expression string.

predicate(value, predicate)

@spec predicate(term(), binary()) :: term()

Applies a conditional predicate such as present? or nil? to value.

Unknown predicates fall back to a lookup, so a context value can expose its own flags.

to_string(arg1)

@spec to_string(t()) :: binary()

Renders an expression the way sablon reports it in errors, e.g. «post.title».