Struct Context
pub struct Context { /* private fields */ }
The struct that holds the context of a template rendering.
Light wrapper around a BTreeMap for easier insertions of Serializable
values
Implementations
impl Context
fn new() -> SelfInitializes an empty context
fn from_serialize<T: Serialize + ?Sized>(value: &T) -> TeraResult<Self>Takes something that implements Serialize and creates a context with it. Meant to be used if you have a hashmap or a struct and don't want to insert values one by one in the context.
fn insert<S: Into<Cow<'static, str>>, T: Serialize + ?Sized>(&mut self, key: S, val: &T)Converts the
valparameter toValueand insert it into the context. This can panic if the value cannot be serialised.# use tera::Context; let mut context = tera::Context::new(); context.insert("number_users", &42);fn insert_value<S: Into<Cow<'static, str>>>(&mut self, key: S, val: Value)In case you already have a
Valueyou want to insertfn remove(&mut self, key: &str) -> Option<Value>Remove a key from the context, returning the value at the key if the key was previously inserted into the context.
fn extend(&mut self, source: Context)Appends the data of the
sourceparameter toself, overwriting existing keys. The source context will be dropped.# use tera::Context; let mut target = Context::new(); target.insert("a", &1); target.insert("b", &2); let mut source = Context::new(); source.insert("b", &3); source.insert("d", &4); target.extend(source);fn contains_key(&self, key: &str) -> boolChecks if a value exists for given key.
fn get(&self, key: &str) -> Option<&Value>Returns the value at the given key
Trait Implementations
impl Clone for Context
fn clone(&self) -> Self
impl Debug for Context
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Context
fn default() -> Self
impl PartialEq for Context
fn eq(&self, other: &Self) -> bool