Struct Context

struct Context { ... }

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 insert<T: Serialize + ?Sized, S: Into<String>>(self: &mut Self, key: S, val: &T)

Converts the val parameter to Value and insert it into the context.

Panics if the serialization fails.

# use tera::Context;
let mut context = tera::Context::new();
context.insert("number_users", &42);
fn try_insert<T: Serialize + ?Sized, S: Into<String>>(self: &mut Self, key: S, val: &T) -> TeraResult<()>

Converts the val parameter to Value and insert it into the context.

Returns an error if the serialization fails.

# use tera::Context;
# struct CannotBeSerialized;
# impl serde::Serialize for CannotBeSerialized {
#     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
#         Err(serde::ser::Error::custom("Error"))
#     }
# }
# let user = CannotBeSerialized;
let mut context = Context::new();
// user is an instance of a struct implementing `Serialize`
if let Err(_) = context.try_insert("number_users", &user) {
    // Serialization failed
}
fn extend(self: &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 into_json(self: Self) -> Value

Converts the context to a serde_json::Value consuming the context.

fn from_value(obj: Value) -> TeraResult<Self>

Takes a serde-json Value and convert it into a Context with no overhead/cloning.

fn from_serialize<impl Serialize: Serialize>(value: impl Serialize) -> TeraResult<Self>

Takes something that impl Serialize and create 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 get(self: &Self, index: &str) -> Option<&Value>

Returns the value at a given key index.

fn remove(self: &mut Self, index: &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 contains_key(self: &Self, index: &str) -> bool

Checks if a value exists at a specific index.

impl Clone for Context

fn clone(self: &Self) -> Context

impl Debug for Context

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

impl Default for Context

fn default() -> Context

impl Freeze for Context

impl PartialEq for Context

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

impl RefUnwindSafe for Context

impl Send for Context

impl StructuralPartialEq for Context

impl Sync for Context

impl Unpin for Context

impl UnsafeUnpin for Context

impl UnwindSafe for Context

impl<T> Any for Context

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Context

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Context

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

impl<T> CloneToUninit for Context

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

impl<T> From for Context

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Pointable for Context

unsafe fn init(init: <T as Pointable>::Init) -> usize
unsafe fn deref<'a>(ptr: usize) -> &'a T
unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
unsafe fn drop(ptr: usize)

impl<T> ToOwned for Context

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Context

fn into(self: 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 for Context

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Context

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

impl<V, T> VZip for Context

fn vzip(self: Self) -> V