pub struct Context { /* private fields */ }Expand description
Template rendering context — holds all variables available during rendering.
§Examples
From an iterator of tuples:
use md_tmpl_core::{Context, Value};
let ctx: Context = vec![
("name", Value::from("Alice")),
("count", Value::from(3_i64)),
]
.into_iter()
.collect();
assert!(ctx.get("name").is_some());Implementations§
Source§impl Context
impl Context
Sourcepub fn with_capacity(capacity: usize) -> Context
pub fn with_capacity(capacity: usize) -> Context
Create an empty context pre-allocated for capacity variables.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if a variable with the given key exists.
Sourcepub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>)
pub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>)
Insert a value into the context.
This is the dynamic API — the key is a plain string and type
mismatches are caught at render_ctx() time,
not here. For compile-time type safety, prefer one of:
include_template!(frommd-tmpl-macros) — generates a strongly-typed parameter struct from your template.Template::render(featureserde) — renders directly from anySerializestruct.
§Panics
Panics if key is the reserved internal key __kind__ — this key
is used internally for enum variant tagging and must not be set
directly. Use enum types in the template frontmatter instead.
Sourcepub fn var(self, key: impl Into<String>, value: impl Into<Value>) -> Context
pub fn var(self, key: impl Into<String>, value: impl Into<Value>) -> Context
Builder-style insert — returns self for chaining.
Sourcepub fn into_inner(self) -> HashMap<String, Value>
pub fn into_inner(self) -> HashMap<String, Value>
Consume this context and return the inner variable map.
Useful for converting a context into a Value::Struct.
Source§impl Context
impl Context
Sourcepub fn from_value(val: Value) -> Result<Context, TemplateError>
pub fn from_value(val: Value) -> Result<Context, TemplateError>
Build a Context directly from a Value::Struct.
§Errors
Returns TemplateError::Syntax if the value is not a dict/map.
Sourcepub fn from_serialize<T>(value: &T) -> Result<Context, TemplateError>where
T: Serialize,
pub fn from_serialize<T>(value: &T) -> Result<Context, TemplateError>where
T: Serialize,
Build a Context from any Serialize type that serializes as a map/struct.
§Errors
Returns TemplateError::Syntax if the serialized value is not a dict/map.
Source§impl Context
CBOR support — behind the cbor feature, which implies serde.
impl Context
CBOR support — behind the cbor feature, which implies serde.
Available in no_std — ciborium uses its own Read trait with a
blanket impl for &[u8].
Source§impl Context
FlexBuffers support — behind the flexbuffers feature, which implies
std and serde (the flexbuffers crate does not support no_std).
impl Context
FlexBuffers support — behind the flexbuffers feature, which implies
std and serde (the flexbuffers crate does not support no_std).
Sourcepub fn from_flexbuffers(data: &[u8]) -> Result<Context, TemplateError>
pub fn from_flexbuffers(data: &[u8]) -> Result<Context, TemplateError>
Build a Context from a FlexBuffers binary buffer.
§Errors
Returns TemplateError::Syntax if the buffer is invalid or not a dict/map.