Module minijinja::value[][src]

Expand description

Provides a dynamic value type abstraction.

This module gives access to a dynamically typed value which is used by the template engine during execution.

For the most part the existence of the value type can be ignored as MiniJinja will perform the necessary conversions for you. For instance if you write a filter that converts a string you can directly declare the filter to take a String. However for some more advanced use cases it’s useful to know that this type exists.

Converting Values

Values are typically created via the From trait:

let value = Value::from(42);

MiniJinja will however create values via an indirection via serde when a template is rendered or an expression is evaluated. This can also be triggered manually by using the Value::from_serializable method:

let value = Value::from_serializable(&[1, 2, 3]);

To to into the inverse directly the various TryFrom implementations can be used:

use std::convert::TryFrom;
let v = u64::try_from(Value::from(42)).unwrap();

Value Function Arguments

Filters and tests can take values as arguments but optionally also rust types directly. This conversion for function arguments is performed by the FunctionArgs trait.

Memory Management

Values are immutable objects which are internally reference counted which means they can be copied relatively cheaply. Special care must be taken so that cycles are not created to avoid causing memory leaks.

HTML Escaping

MiniJinja inherits the general desire to be clever about escaping. For this prupose a value will (when auto escaping is enabled) always be escaped. To prevent this behavior the safe filter can be used in the template. Outside of templates the Value::from_safe_string method can be used to achieve the same result.

Dynamic Objects

Values can also hold “dynamic” objects. These are objects which implement the Object trait. These can be used to implement dynamic functionality such as stateful values and more.

Structs

SingleDeprecated

Helper to pass a single value as context bound to a name.

Represents a dynamically typed value in the template engine.

Enums

An alternative view of a value.

Describes the kind of value.

Traits

A trait implemented by all filter/test argument types.

Helper trait representing valid filter and test arguments.

A utility trait that represents a dynamic object.