pub fn render_with_context<T: Serialize>(
template: &str,
data: &T,
theme: &Theme,
mode: OutputMode,
context_registry: &ContextRegistry,
render_context: &RenderContext<'_>,
template_registry: Option<&TemplateRegistry>,
) -> Result<String, RenderError>Expand description
Renders a template with additional context objects injected.
This is the most flexible rendering function, allowing you to inject additional objects into the template context beyond the serialized data. Use this when templates need access to utilities, formatters, or runtime values that cannot be represented as JSON.
§Arguments
template- A minijinja template stringdata- Any serializable data to pass to the templatetheme- Theme definitions for thestylefiltermode- Output mode:Auto,Term,Text, etc.context_registry- Additional context objects to injectrender_context- Information about the render environment
§Context Resolution
Context objects are resolved from the registry using the provided
RenderContext. Each registered provider is called to produce a value,
which is then merged into the template context.
If a context key conflicts with a data field, the data field wins. Context is supplementary to the handler’s data, not a replacement.
§Example
use standout_render::{render_with_context, Theme, OutputMode};
use standout_render::context::{RenderContext, ContextRegistry};
use minijinja::Value;
use serde::Serialize;
#[derive(Serialize)]
struct Data { name: String }
let theme = Theme::new();
let data = Data { name: "Alice".into() };
// Create context with a static value
let mut registry = ContextRegistry::new();
registry.add_static("version", Value::from("1.0.0"));
// Create render context
let json_data = serde_json::to_value(&data).unwrap();
let render_ctx = RenderContext::new(
OutputMode::Text,
Some(80),
&theme,
&json_data,
);
let output = render_with_context(
"{{ name }} (v{{ version }})",
&data,
&theme,
OutputMode::Text,
®istry,
&render_ctx,
None,
).unwrap();
assert_eq!(output, "Alice (v1.0.0)");