Struct trillium_tera::Context

source ·
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

source

pub fn new() -> Context

Initializes an empty context

source

pub fn insert<T, S>(&mut self, key: S, val: &T)
where T: Serialize + ?Sized, S: Into<String>,

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

Panics if the serialization fails.

let mut context = tera::Context::new();
context.insert("number_users", &42);
source

pub fn try_insert<T, S>(&mut self, key: S, val: &T) -> Result<(), Error>
where T: Serialize + ?Sized, S: Into<String>,

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

Returns an error if the serialization fails.

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
}
source

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);
source

pub fn into_json(self) -> Value

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

source

pub fn from_value(obj: Value) -> Result<Context, Error>

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

source

pub fn from_serialize(value: impl Serialize) -> Result<Context, Error>

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.

source

pub fn get(&self, index: &str) -> Option<&Value>

Returns the value at a given key index.

source

pub fn remove(&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.

source

pub fn contains_key(&self, index: &str) -> bool

Checks if a value exists at a specific index.

Trait Implementations§

source§

impl Clone for Context

source§

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Context

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Context

source§

fn default() -> Context

Returns the “default value” for a type. Read more
source§

impl PartialEq for Context

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Context

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

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> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V