Skip to main content

render_with_context

Function render_with_context 

Source
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, Error>
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 string
  • data - Any serializable data to pass to the template
  • theme - Theme definitions for the style filter
  • mode - Output mode: Auto, Term, Text, etc.
  • context_registry - Additional context objects to inject
  • render_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_with_context, Theme, OutputMode};
use standout::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,
    &registry,
    &render_ctx,
    None,
).unwrap();

assert_eq!(output, "Alice (v1.0.0)");