Sablon.Statement (sablon v0.4.4)

Copy Markdown View Source

The operations a template can perform.

Statements are built once from the merge fields found in a document part and then evaluated in order, each mutating the document in place.

Summary

Functions

Evaluates a statement against env, mutating the document.

Renders the first clause whose condition holds and clears the rest.

The value a clause's truthiness is decided by: its expression, optionally through a predicate.

The truthiness rules sablon uses: nil, false and empty lists are falsy.

Functions

evaluate(statement, env)

@spec evaluate(
  struct(),
  Sablon.Environment.t()
) :: any()

Evaluates a statement against env, mutating the document.

Dispatch is by struct module, so an application can define its own statement by giving the module an evaluate/2 function - see Sablon.Processor.Document.register_field_handler/2 for how to produce one.

render_conditions(conditions, else_block, env, resolve \\ &__MODULE__.resolve_condition/2)

@spec render_conditions([map()], term(), Sablon.Environment.t(), (map(),
                                                            Sablon.Environment.t() ->
                                                              term())) :: any()

Renders the first clause whose condition holds and clears the rest.

resolve turns a clause into the value its truthiness is decided by; the default reads the clause expression from the context and applies its predicate. Passing a different one is how a custom conditional - comparison operators, say - reuses the rest of this logic.

resolve_condition(condition, env)

@spec resolve_condition(map(), Sablon.Environment.t()) :: term()

The value a clause's truthiness is decided by: its expression, optionally through a predicate.

truthy?(value)

@spec truthy?(term()) :: boolean()

The truthiness rules sablon uses: nil, false and empty lists are falsy.