Struct liquid::Context
[−]
[src]
pub struct Context { pub filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>, // some fields omitted }
Fields
filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>
Methods
impl Context
[src]
fn new() -> Context
Creates a new, empty rendering context.
Examples
let ctx = Context::new(); assert_eq!(ctx.get_val("test"), None);
fn with_values(values: HashMap<String, Value>) -> Context
fn with_filters(filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>)
-> Context
-> Context
fn with_values_and_filters(values: HashMap<String, Value>,
filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>)
-> Context
filters: HashMap<String, Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>)
-> Context
fn cycle_element(&mut self,
name: &str,
values: &[Token])
-> Result<Option<Value>, Error>
name: &str,
values: &[Token])
-> Result<Option<Value>, Error>
fn maybe_add_filter(&mut self,
name: &str,
filter: Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>)
name: &str,
filter: Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>)
Only add the given filter to the context if a filter with this name doesn't already exist.
fn add_filter(&mut self,
name: &str,
filter: Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>)
name: &str,
filter: Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>)
fn get_filter<'b>(&'b self,
name: &str)
-> Option<&'b Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>
name: &str)
-> Option<&'b Box<Fn(&Value, &[Value]) -> Result<Value, FilterError>>>
fn interrupted(&self) -> bool
fn set_interrupt(&mut self, interrupt: Interrupt)
Sets the interrupt state. Any previous state is obliterated.
fn pop_interrupt(&mut self) -> Option<Interrupt>
Fetches and clears the interrupt state.
fn run_in_scope<RvalT, FnT>(&mut self, f: FnT) -> RvalT where FnT: FnOnce(&mut Context) -> RvalT
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)));
fn get_val<'b>(&'b self, name: &str) -> Option<&'b Value>
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));
fn set_val(&mut self, name: &str, val: Value) -> Option<Value>
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)));
fn evaluate(&self, t: &Token) -> Result<Option<Value>, Error>
Translates a Token to a Value, looking it up in the context if necessary
fn set_local_val(&mut self, name: &str, val: Value) -> Option<Value>
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);