pub struct Context { /* private fields */ }Expand description
The struct that holds the context of a template rendering.
Light wrapper around a BTreeMap for easier insertions of Serializable
values
Implementations§
Source§impl Context
impl Context
Sourcepub fn from_serialize<T: Serialize + ?Sized>(value: &T) -> TeraResult<Self>
pub 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.
Sourcepub fn insert<S: Into<Cow<'static, str>>, T: Serialize + ?Sized>(
&mut self,
key: S,
val: &T,
)
pub fn insert<S: Into<Cow<'static, str>>, T: Serialize + ?Sized>( &mut self, key: S, val: &T, )
Converts the val parameter to Value and insert it into the context.
let mut context = tera::Context::new();
context.insert("number_users", &42);Sourcepub fn insert_value<S: Into<Cow<'static, str>>>(&mut self, key: S, val: Value)
pub fn insert_value<S: Into<Cow<'static, str>>>(&mut self, key: S, val: Value)
In case you already have a Value you want to insert
Sourcepub fn remove(&mut self, key: &str) -> Option<Value>
pub fn 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.
Sourcepub fn extend(&mut self, source: Context)
pub fn extend(&mut self, source: Context)
Appends the data of the source parameter to self, overwriting existing keys.
The source context will be dropped.
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);Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Checks if a value exists for given key.
Trait Implementations§
impl StructuralPartialEq for Context
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more