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() -> Self

Initializes 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 val parameter to Value and 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 Value you want to insert

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.

fn extend(&mut self, source: Context)

Appends the data of the source parameter to self, 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) -> bool

Checks 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) -> Context

impl Debug for Context

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Default for Context

fn default() -> Context

impl PartialEq for Context

fn eq(&self, other: &Context) -> bool

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

impl<T> Any for Context where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Context where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Context where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Context where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Context

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Context where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Context where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Context where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Context where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>