3 releases
Uses new Rust 2024
new 0.1.2 | May 10, 2025 |
---|---|
0.1.1 | May 10, 2025 |
0.1.0 | May 10, 2025 |
#12 in #condition
30KB
546 lines
Templi
A lightweight, fast, and intuitive template engine for Rust. Highly inspired by Jinja2.
Table of Contents
Overview
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.
Features
- Simple and intuitive syntax
- Variable interpolation
- Conditional rendering with
if
,elif
, andelse
- Loop through arrays with
each
- Nested property access
- Comparison operators (
==
,!=
,>
,<
,>=
,<=
) - Existence checks for variables
Project Structure
└── templi/
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
└── src
├── lib.rs
└── errors.rs
└── parser.rs
└── renderer.rs
Getting Started
Installation
Add Templi to your Cargo.toml
:
[dependencies]
templi = "0.1.2"
Usage
Basic Example
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!
}
Using Variables
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();
Accessing Nested Properties
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();
Conditional Rendering
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
Using Else If
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
Checking If Variable Exists
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!
Iterating Over Arrays
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>
Syntax
Variables
{{variable_name}}
- Interpolates the value of the variable{{object.property}}
- Accesses nested properties
Directives
{{@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
Conditions
{{@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 comparison
Contributing
- 🐛 Report Issues: Submit bugs found or log feature requests for the
migro
project. - 💡 Submit Pull Requests: Review open PRs, and submit your own PRs.
Contributing Guidelines
- Fork the Repository: Start by forking the project repository to your github account.
- Clone Locally: Clone the forked repository to your local machine using a git client.
git clone https://github.com/mstjr/templi
- Create a New Branch: Always work on a new branch, giving it a descriptive name.
git checkout -b new-feature-x
- Make Your Changes: Develop and test your changes locally.
- Commit Your Changes: Commit with a clear message describing your updates.
git commit -m 'Implemented new feature x.'
- Push to github: Push the changes to your forked repository.
git push origin new-feature-x
- Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
- Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
License
This project is licensed under the MIT license (LICENSE-MIT)
Dependencies
~2.6–4MB
~73K SLoC