Expand description
§Figura
Figura is a template engine for Rust that supports variable substitution, repeating patterns, and conditional logic with customizable delimiters.
If you don’t like that, you can extend the default parser by implementing the Parser trait.
§Features
- Variable substitution:
{name}- Replace with context values - Repeating patterns:
{pattern:count}- Repeat a pattern N times - Conditionals:
{condition ? true_value : false_value}- Ternary expressions - Comparisons: Support for
==,!=,>,<,>=,<= - Custom Logic: You can implement custom logic using the
LogicandParsertraits - Custom delimiters: Use any characters as open/close delimiters
- Zero-copy where possible: Leverages
Cowfor efficiency
§Example
use figura::{Template, Context, Value};
use std::collections::HashMap;
// Create a context with variables
let mut ctx = HashMap::new();
ctx.insert("name", Value::static_str("World"));
ctx.insert("count", Value::Int(3));
// Compile a template (using default '{' and '}' delimiters)
let template = Template::<'{', '}'>::compile(
"Hello {name}! {'*':count}"
).unwrap();
// Render the template
let result = template.format(&ctx).unwrap();
assert_eq!(result, "Hello World! ***");Structs§
- Conditional
Directive - A directive that performs conditional branching (ternary operator).
- Default
Parser - The default parser implementation.
- Empty
Directive - A directive that produces no output.
- Literal
Directive - A directive that outputs a literal string.
- Repeat
Directive - A directive that repeats a pattern a specified number of times.
- Replace
Directive - A directive that substitutes a variable or evaluates an expression.
- Template
- A compiled template ready for rendering.
- Template
Lexer
Enums§
- Argument
- An argument that can be resolved to a value at runtime.
- Comparison
Op - Comparison operators for use in conditional expressions.
- Directive
Error - Errors that can occur during directive execution.
- Expression
- An expression that can be evaluated to produce a value.
- Template
Error - Token
- Value
- A runtime value that can be stored in the template context.
Traits§
- Directive
- A template directive that can be executed to produce output.
- Parser
- A parser that converts token sequences into executable directives.
- Resolvable
- Types that can be resolved from template arguments.
Type Aliases§
- Context
- The context passed to templates during rendering.