Skip to main content

render_auto_with_context

Function render_auto_with_context 

Source
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 serialize
  • theme - Theme definitions for the style filter
  • mode - Output mode determining the output format
  • context_registry - Additional context objects to inject
  • render_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,
    &registry,
    &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,
    &registry,
    &render_ctx,
    None,
).unwrap();
assert!(json.contains("\"title\": \"Summary\""));