Figura - Lightweight Template Engine for Rust
.---------------.
| J |
| . //// |
| / \ |o o| |
| (_,_) | < | |
| | |___| |
| / \ |
| | ||
'---------------'
Features
- Variable Substitution - Replace placeholders with context values
- Pattern Repetition - Repeat strings a specified number of times
- Conditionals - Ternary operators with comparison support
- Custom Delimiters - Use any characters as template boundaries
- Extensible Parsers - Implement custom parsing logic
- Zero-Copy - Efficient string handling with
Cow - Escape Sequences - Support for literal delimiter characters
Installation
Add this to your Cargo.toml:
[]
= "2.0.3"
Quick Start
use ;
use HashMap;
let mut ctx = new;
ctx.insert;
ctx.insert;
let template = ', '}'>compile.unwrap;
let output = template.format.unwrap;
assert_eq!;
Syntax
Variable Substitution
let template = ', '}'>compile.unwrap;
ctx.insert;
ctx.insert;
// Output: "User: Bob, Age: 25"
Literals
let template = ', '}'>compile.unwrap;
// Output: "Message: Hello World"
Pattern Repetition
let template = ', '}'>compile.unwrap;
ctx.insert;
// Output:
// --------------------------------------------------
// HEADER
// --------------------------------------------------
Conditionals
Simple boolean conditions:
let template = ', '}'>compile.unwrap;
ctx.insert;
// Output: "Status: Online"
With comparisons:
let template = ', '}'>compile.unwrap;
ctx.insert;
// Output: "Access: Granted"
Supported operators: ==, !=, >, <, >=, <=
Logical NOT:
let template = ', '}'>compile.unwrap;
Escaped Delimiters
let template = ', '}'>compile.unwrap;
// Output: "Literal braces: {not a variable}"
Custom Delimiters
Use any characters as delimiters:
// Angle brackets
let template = compile.unwrap;
// Square brackets
let template = compile.unwrap;
// Same character for both
let template = ', '%'>compile.unwrap;
Value Types
Figura supports four value types:
// String (zero-copy when possible)
ctx.insert;
ctx.insert;
// Integer
ctx.insert;
// Float
ctx.insert;
// Boolean
ctx.insert;
Custom Parsers
Implement the Parser trait to create custom parsing logic:
use ;
;
// Implement custom directive
// Use custom parser
let template = ', '}'>.unwrap;
API Overview
Core Types
Template<O, C>- Compiled template with open/close delimitersValue- Runtime values (String, Int, Float, Bool)Context- HashMap of variable names to valuesDefaultParser- Built-in parser implementationParser- Trait for custom parsersDirective- Trait for executable template components
License
MIT License - Copyright (c) Saverio Scagnoli
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.