| Crates.io | templi |
| lib.rs | templi |
| version | 0.1.2 |
| created_at | 2025-05-10 15:13:29.121755+00 |
| updated_at | 2025-05-10 15:15:29.20754+00 |
| description | A template engine for Rust that is fast, simple, and easy to use. Highly inspired by Jinja2. |
| homepage | |
| repository | https://github.com/mstjr/templi |
| max_upload_size | |
| id | 1668451 |
| size | 33,993 |
A lightweight, fast, and intuitive template engine for Rust. Highly inspired by Jinja2.
Templi is a lightweight and fast template engine for Rust, designed to be simple and intuitive. It allows you to create dynamic templates with ease, using a syntax similar to Jinja2. Templi supports variable interpolation, conditional rendering, and looping through arrays, making it a powerful tool for generating HTML or other text-based formats.
Templi is built with performance in mind, leveraging Rust's strengths to provide a fast and efficient templating solution. Whether you're building web applications, generating reports, or creating configuration files, Templi can help you streamline your templating process.
if, elif, and elseeach==, !=, >, <, >=, <=)└── templi/
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
└── src
├── lib.rs
└── errors.rs
└── parser.rs
└── renderer.rs
Add Templi to your Cargo.toml:
[dependencies]
templi = "0.1.2"
If you don't want to use serde_json
use templi::render_from_str;
fn main() {
let template = "Hello, {{name}}!";
let context = r#"{"name": "World"}"#;
let result = render_from_str(template, context).unwrap();
println!("{}", result); // Outputs: Hello, World!
}
If you want to use serde_json
use templi::render;
use serde_json::json;
fn main() {
let template = "Hello, {{name}}!";
let context = json!({ "name": "World" });
let result = render(template, context).unwrap();
println!("{}", result); // Outputs: Hello, World!
}
use templi::render;
use serde_json::json;
let template = "<h1>{{title}}</h1><p>{{content}}</p>";
let context = json!({
"title": "Welcome to Templi",
"content": "A template engine for Rust"
});
let result = render(template, context).unwrap();
use templi::render;
use serde_json::json;
let template = "{{user.name}} lives in {{user.address.city}}";
let context = json!({
"user": {
"name": "John",
"address": {
"city": "New York"
}
}
});
let result = render(template, context).unwrap();
use templi::render;
use serde_json::json;
let template = "{{@if age > 18}}Adult{{@else}}Minor{{@endif}}";
let context = json!({ "age": 25 });
let result = render(template, context).unwrap(); // Outputs: Adult
use templi::render;
use serde_json::json;
let template = "{{@if age > 18}}Adult{{@elif age > 12}}Teen{{@else}}Child{{@endif}}";
let context = json!({ "age": 15 });
let result = render(template, context).unwrap(); // Outputs: Teen
use templi::render;
use serde_json::json;
let template = "{{@if name exists}}Hello, {{name}}!{{@else}}Hello, Guest!{{@endif}}";
let context = json!({ "age": 25 });
let result = render(template, context).unwrap(); // Outputs: Hello, Guest!
use templi::render;
use serde_json::json;
let template = "<ul>{{@each items as item}}<li>{{item}}</li>{{@endeach}}</ul>";
let context = json!({ "items": ["Apple", "Banana", "Cherry"] });
let result = render(template, context).unwrap();
// Outputs: <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
{{variable_name}} - Interpolates the value of the variable{{object.property}} - Accesses nested properties{{@if condition}}...{{@endif}} - Conditional rendering{{@if condition}}...{{@else}}...{{@endif}} - If-else conditional{{@if condition}}...{{@elif condition}}...{{@else}}...{{@endif}} - If-elif-else conditional{{@each array as item}}...{{@endeach}} - Loop through an array{{@if variable exists}} - Checks if a variable exists and is not null{{@if a == b}} - Equality comparison{{@if a != b}} - Inequality comparison{{@if a > b}} - Greater than comparison{{@if a < b}} - Less than comparison{{@if a >= b}} - Greater than or equal comparison{{@if a <= b}} - Less than or equal comparisonmigro project.git clone https://github.com/mstjr/templi
git checkout -b new-feature-x
git commit -m 'Implemented new feature x.'
git push origin new-feature-x
This project is licensed under the MIT license (LICENSE-MIT)