[][src]Module bracket::helper

Helper trait and types for the default set of helpers.

The Helper Trait should be implemented for custom helpers which can then be added to a registry.

Helper call() functions accept three arguments:

  • rc The active renderer.
  • ctx Helper arguments and hash parameters.
  • template Inner template when called as a block.

The renderer can be used to render inner templates when a helper is called as a block and provides functions for writing to the output destination.

The context is used to access the arguments and hash parameters and may also be used for type assertions.

When a helper is called as a block the optional template node will be Some. Raw helpers can access the inner text using text().

To determine how a helper was invoked requires checking for an inner template or raw text; if neither is available it is a statement:

This example is not tested
if let Some(node) = template {
    // Helper was invoked as a block `{{#helper}}...{{/helper}}`
} else if let Some(text) = ctx.text() {
    // Helper was invoked as a raw block `{{{{helper}}}}...{{{{/helper}}}}`
} else {
    // Helper was invoked as a statement `{{helper}}`
}

Type Assertions

Type assertions let us verify the type of helper arguments and hash parameters before we use them.

The arity() method is used to assert on argument length.

Use try_get() to get an argument and verify it is an expected type.

Use try_param() to get a hash parameter and verify it is an expected type.

Return Values

The signature for helper return values is HelperValue which requires that the call() function returns an optional Value.

A return value is useful when a helper is invoked as a statement; when invoked as a block return Ok(None).

If a statement helper is used for side-effects (such as the Log helper) then return Ok(None).

Local Helpers

Local helpers are defined on rc using register_local_helper() and live for the lifetime of the parent helper call.

They must implement the LocalHelper Trait which has an additional bounds on Clone.

Render

To render an inner template when a helper is called as a block use template() which will respect the current whitespace trim hints:

This example is not tested
if let Some(node) = template {
   rc.template(node)?;
}

To buffer() the content of an inner template into a string:

This example is not tested
if let Some(node) = template {
   let content = rc.buffer(node)?;
}

Modules

comparison

Helpers for numerical comparisons.

each

Block helper that iterates arrays and objects.

if

Helpers for conditional statements.

json

Helper that returns a JSON string.

log

Helper to print log messages.

logical

Helpers for conditional statements.

lookup

Helper to lookup a field of an array or object.

prelude

Prelude for helper definitions.

unless

Block helper for negated conditional.

with

Block helper that sets the scope.

Structs

HelperRegistry

Collection of helpers.

Traits

Helper

Trait for helpers.

LocalHelper

Trait for local helpers which must implement Clone.

Type Definitions

HelperResult

Result type returned when invoking helpers.

HelperValue

Result type that helper implementations should return.