docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! # Batch Render — Parse Once, Render Many
//!
//! Demonstrates the key architectural insight of docx-templating:
//! `Template` is parsed **once at load time** and can be rendered against
//! any number of contexts without reparsing.
//!
//! Generates one personalised renewal letter per recipient.
//!
//! **First run:** `cargo run --bin gen-examples`
//! **Then:** `cargo run --example 10_batch_render`

use docx_templating::{Context, Template};
use serde::Serialize;
use std::path::Path;

#[derive(Serialize)]
struct Recipient {
    name: String,
    company: String,
    ref_number: String,
    renewal_date: String,
    plan: String,
}

fn recipients() -> Vec<Recipient> {
    vec![
        Recipient {
            name: "Ada Lovelace".into(),
            company: "Analytical Engines Ltd".into(),
            ref_number: "REF-001".into(),
            renewal_date: "2026-09-01".into(),
            plan: "Enterprise".into(),
        },
        Recipient {
            name: "Grace Hopper".into(),
            company: "Compilers Inc".into(),
            ref_number: "REF-002".into(),
            renewal_date: "2026-09-15".into(),
            plan: "Professional".into(),
        },
        Recipient {
            name: "Alan Turing".into(),
            company: "Enigma Solutions".into(),
            ref_number: "REF-003".into(),
            renewal_date: "2026-10-01".into(),
            plan: "Starter".into(),
        },
    ]
}

fn main() {
    // Parse ONCE — all render() calls below reuse this parsed state.
    let template = Template::from_path("examples/input/renewal-letter.docx")
        .expect("template not found — run `cargo run --bin gen-examples` first");

    let out_dir = Path::new("examples/output");
    std::fs::create_dir_all(out_dir).expect("create output dir");

    let all = recipients();
    let total = all.len();

    for (i, recipient) in all.iter().enumerate() {
        let ctx = Context::from_serialize(recipient).expect("serialize");
        let rendered = template.render(ctx).expect("render");
        let out_path = out_dir.join(format!("letter-{:02}.docx", i + 1));
        rendered.write_to_file(&out_path).expect("write");
        println!(
            "[{}/{}] {} -> {}",
            i + 1,
            total,
            recipient.name,
            out_path.display()
        );
    }

    println!("\n{total} letters generated from a single parsed template.");
}