#tera #anvil #file #strong-typing #anvil-tera

anvil-tera

Anvil wrapper for the tera templating library

4 releases

new 0.2.2 Apr 12, 2025
0.2.1 Apr 12, 2025
0.1.1 Apr 4, 2025
0.1.0 Apr 2, 2025

#720 in Template engine

Download history 180/week @ 2025-03-29 103/week @ 2025-04-05

283 downloads per month

MIT license

74KB
757 lines

anvil-tera

An Anvil integration for Tera templates.

Installation

[dependencies]
anvil-tera = "0.2.1"
anvil = "0.3.0"
tera = "1.20.0"
serde = { version = "1.0", features = ["derive"] }

Usage

use anvil::Forge;
use anvil_tera::prelude::*;  // Import extension traits and functions
use serde::Serialize;
use tera::Tera;
use std::sync::LazyLock;

// Define a Tera instance for your templates
static TEMPLATES: LazyLock<Tera> = LazyLock::new(|| {
    let mut tera = Tera::default();
    tera.add_raw_template("greeting.html", "Hello, {{ name }}!").unwrap();
    tera
});

// Define a serializable template context
#[derive(Serialize)]
struct MyTemplate {
    name: String,
}

// Use macro to implement the Earth trait with a template
make_tera_template!(MyTemplate, "greeting.html", TEMPLATES);

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let template = MyTemplate { name: "World".to_string() };

    // Generate a new file
    generate(&template).forge("hello.txt")?;

    // Append to an existing file
    append(&template).forge("log.txt")?;

    Ok(())
}

Dependencies

~7–16MB
~202K SLoC