Struct liquid::Context [] [src]

pub struct Context {
    pub filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>,
    // some fields omitted
}

Fields

Methods

impl Context
[src]

Creates a new, empty rendering context.

Examples

let ctx = Context::new();
assert_eq!(ctx.get_val("test"), None);

Only add the given filter to the context if a filter with this name doesn't already exist.

Sets the interrupt state. Any previous state is obliterated.

Fetches and clears the interrupt state.

Sets up a new stack frame, executes the supplied function and then tears the stack frame down before returning the function's result to the caller.

Examples

let mut ctx = Context::new();
ctx.set_val("test", Value::Num(42f32));
ctx.run_in_scope(|mut stack_frame| {
  // stack_frame inherits values from its parent context
  assert_eq!(stack_frame.get_val("test"), Some(&Value::Num(42f32)));

  // but can (optionally) override them
  stack_frame.set_local_val("test", Value::Num(3.14f32));
  assert_eq!(stack_frame.get_val("test"), Some(&Value::Num(3.14f32)));
});
// the original value is unchanged once the scope exits
assert_eq!(ctx.get_val("test"), Some(&Value::Num(42f32)));

Gets a value from the rendering context. The name value can be a dot-separated path to a value. A value will only be returned if each link in the chain (excluding the final name) refers to a value of type Object.

Examples

let mut ctx = Context::new();
ctx.set_val("test", Value::Num(42f32));
assert_eq!(ctx.get_val("test").unwrap(), &Value::Num(42f32));

Sets a value in the global context.

Examples

let mut ctx = Context::new();
ctx.set_val("test", Value::Num(42f32));
assert_eq!(ctx.get_val("test"), Some(&Value::Num(42f32)));

Translates a Token to a Value, looking it up in the context if necessary

Sets a value to the rendering context. Note that it needs to be wrapped in a liquid::Value.

Panics

Panics if there is no frame on the local values stack. Context instances are created with a top-level stack frame in place, so this should never happen in a well-formed program.

Examples

let mut ctx = Context::new();
ctx.run_in_scope(|mut local_scope| {
  local_scope.set_val("global", Value::Num(42f32));
  local_scope.set_local_val("local", Value::Num(163f32));

  assert_eq!(local_scope.get_val("global"), Some(&Value::Num(42f32)));
  assert_eq!(local_scope.get_val("local"), Some(&Value::Num(163f32)));
});
assert_eq!(ctx.get_val("global"), Some(&Value::Num(42f32)));
assert_eq!(ctx.get_val("local"), None);

Trait Implementations

impl Default for Context
[src]

Returns the "default value" for a type. Read more