pub fn render_auto_with_context<T: Serialize>(
template: &str,
data: &T,
theme: &Theme,
mode: OutputMode,
context_registry: &ContextRegistry,
render_context: &RenderContext<'_>,
template_registry: Option<&TemplateRegistry>,
) -> Result<String, Error>Expand description
Auto-dispatches with context injection support.
This combines render_with_context with JSON serialization support.
For structured modes like Json, the data is serialized directly,
skipping template rendering (and context injection).
§Arguments
template- A minijinja template string (ignored for structured modes)data- Any serializable data to render or serializetheme- Theme definitions for thestylefiltermode- Output mode determining the output formatcontext_registry- Additional context objects to injectrender_context- Information about the render environment
§Example
use standout::{render_auto_with_context, Theme, OutputMode};
use standout::context::{RenderContext, ContextRegistry};
use minijinja::Value;
use serde::Serialize;
#[derive(Serialize)]
struct Report { title: String, count: usize }
let theme = Theme::new();
let data = Report { title: "Summary".into(), count: 42 };
let mut registry = ContextRegistry::new();
registry.add_provider("terminal_width", |ctx: &RenderContext| {
Value::from(ctx.terminal_width.unwrap_or(80))
});
let json_data = serde_json::to_value(&data).unwrap();
let render_ctx = RenderContext::new(
OutputMode::Text,
Some(120),
&theme,
&json_data,
);
// Text mode uses the template with context
let text = render_auto_with_context(
"{{ title }} (width={{ terminal_width }}): {{ count }}",
&data,
&theme,
OutputMode::Text,
®istry,
&render_ctx,
None,
).unwrap();
assert_eq!(text, "Summary (width=120): 42");
// JSON mode ignores template and context, serializes data directly
let json = render_auto_with_context(
"unused",
&data,
&theme,
OutputMode::Json,
®istry,
&render_ctx,
None,
).unwrap();
assert!(json.contains("\"title\": \"Summary\""));