/// Example: HTML Page Generation with Tera Templates /// /// This example demonstrates how to use the HtmlRenderer to generate /// complete HTML pages with various layouts and components. use daemon_cli::rendering::{HtmlRenderer, RenderContext}; use serde_json::json; #[tokio::main] async fn main() { println!("═══════════════════════════════════════════════════════════"); println!(" HTML Page Generation Examples with Tera Templates"); println!("═══════════════════════════════════════════════════════════\n"); // Example 1: Basic Page example_basic_page().await; // Example 2: Dashboard with Metrics example_dashboard().await; // Example 3: Report with Table of Contents example_report().await; // Example 4: Component with Loops example_components().await; println!("\n═══════════════════════════════════════════════════════════"); println!(" All examples completed successfully!"); println!("═══════════════════════════════════════════════════════════"); } async fn example_basic_page() { println!("\n1️⃣ BASIC PAGE EXAMPLE"); println!("─────────────────────────────────────────────────────────\n"); let renderer = HtmlRenderer::new(); let template = r#" {{ page_title }}

{{ page_title }}

{{ subtitle }}

Welcome {{ name }}!

{{ message }}

"#; let ctx = RenderContext::new() .with_variable("page_title", json!("Welcome Page")) .with_variable("subtitle", json!("A dynamically generated page")) .with_variable("name", json!("Alice")) .with_variable( "message", json!("This HTML was generated using Tera templates!"), ); let result = renderer.render_html(template, &ctx).await; if result.success { println!("✅ Rendered in {}ms", result.duration_ms); println!("\nGenerated HTML (first 500 chars):"); println!( "{}\n...", &result.output[..std::cmp::min(500, result.output.len())] ); } else { eprintln!("❌ Error: {:?}", result.errors); } } async fn example_dashboard() { println!("\n2️⃣ DASHBOARD EXAMPLE"); println!("─────────────────────────────────────────────────────────\n"); let renderer = HtmlRenderer::new(); let template = HtmlRenderer::dashboard_template(); let metrics = json!([ { "label": "Total Requests", "value": "12,547" }, { "label": "Success Rate", "value": "99.8%" }, { "label": "Avg Response", "value": "45ms" }, { "label": "Errors", "value": "23" } ]); let sections = json!([ { "title": "System Status", "content": "✅ All systems operational. No issues detected." }, { "title": "Recent Activity", "content": "📊 Processing 500+ requests per minute. Database performance optimal." }, { "title": "Deployments", "content": "🚀 Latest deployment: v2.5.1 (2 hours ago)" } ]); let ctx = RenderContext::new() .with_variable("dashboard_title", json!("System Dashboard")) .with_variable("dashboard_subtitle", json!("Real-time monitoring")) .with_variable("metrics", metrics) .with_variable("sections", sections); let result = renderer.render_html(template, &ctx).await; if result.success { println!("✅ Dashboard rendered in {}ms", result.duration_ms); println!("📊 Metrics displayed: 4"); println!("📋 Sections displayed: 3"); println!("💾 HTML size: {} bytes", result.output.len()); } else { eprintln!("❌ Error: {:?}", result.errors); } } async fn example_report() { println!("\n3️⃣ REPORT EXAMPLE"); println!("─────────────────────────────────────────────────────────\n"); let renderer = HtmlRenderer::new(); let template = HtmlRenderer::report_template(); let toc = json!([ { "id": "executive", "title": "Executive Summary" }, { "id": "findings", "title": "Key Findings" }, { "id": "metrics", "title": "Metrics & Statistics" }, { "id": "conclusion", "title": "Conclusion" } ]); let sections = json!([ { "id": "executive", "title": "Executive Summary", "content": "

This report provides a comprehensive analysis of system performance metrics over Q4 2024.

" }, { "id": "findings", "title": "Key Findings", "content": "" }, { "id": "metrics", "title": "Metrics & Statistics", "content": "

Processed: 4.2M requests

Average Response: 52ms

Peak Load: 8,500 req/s

" }, { "id": "conclusion", "title": "Conclusion", "content": "

The system demonstrates excellent stability and performance characteristics. Recommended actions include database optimization and load balancing improvements.

" } ]); let ctx = RenderContext::new() .with_variable("report_title", json!("Q4 2024 Performance Report")) .with_variable("report_author", json!("Operations Team")) .with_variable("toc", toc) .with_variable("sections", sections) .with_variable("footer_text", json!("Confidential - Internal Use Only")); let result = renderer.render_html(template, &ctx).await; if result.success { println!("✅ Report generated in {}ms", result.duration_ms); println!("📄 Sections: 4"); println!("📋 Table of Contents: 4 items"); println!("💾 Report size: {} bytes", result.output.len()); // Save to file if let Err(e) = std::fs::write("example_report.html", &result.output) { eprintln!("⚠️ Could not save to file: {}", e); } else { println!("💾 Saved to: example_report.html"); } } else { eprintln!("❌ Error: {:?}", result.errors); } } async fn example_components() { println!("\n4️⃣ COMPONENTS WITH LOOPS AND CONDITIONALS"); println!("─────────────────────────────────────────────────────────\n"); let renderer = HtmlRenderer::new(); let template = r#" Product List

{{ catalog_title }}

Total products: {{ products | length }}

{% for product in products %}

{{ product.name }}

{{ product.description }}

${{ product.price }}
{% if product.stock > 0 %} {% if product.stock > 10 %} ✅ In Stock ({{ product.stock }} units) {% else %} ⚠️ Low Stock ({{ product.stock }} units) {% endif %} {% else %} ❌ Out of Stock {% endif %} {% if product.featured %}

⭐ Featured Item

{% endif %}
{% endfor %} "#; let products = json!([ { "name": "Laptop Pro", "description": "High-performance laptop for professionals", "price": 1299, "stock": 5, "featured": true }, { "name": "Wireless Mouse", "description": "Ergonomic wireless mouse with long battery life", "price": 29, "stock": 25, "featured": false }, { "name": "USB-C Hub", "description": "7-in-1 USB-C hub with multiple ports", "price": 49, "stock": 2, "featured": false }, { "name": "Monitor Stand", "description": "Adjustable monitor stand", "price": 79, "stock": 0, "featured": false } ]); let ctx = RenderContext::new() .with_variable("catalog_title", json!("Product Catalog")) .with_variable("products", products); let result = renderer.render_html(template, &ctx).await; if result.success { println!("✅ Catalog rendered in {}ms", result.duration_ms); println!("📦 Products displayed: 4"); println!("🌟 Featured items: 1"); if let Err(e) = std::fs::write("example_catalog.html", &result.output) { eprintln!("⚠️ Could not save to file: {}", e); } else { println!("💾 Saved to: example_catalog.html"); } } else { eprintln!("❌ Error: {:?}", result.errors); } }