1 unstable release
0.0.1 | Oct 17, 2024 |
---|
#44 in #ssg
172 downloads per month
Used in 3 crates
(via staticdatagen)
90KB
1K
SLoC
StaticWeaver
A fast and flexible templating engine for Rust applications.
• Website • Documentation • Report Bug • Request Feature • Contributing Guidelines
Overview
staticweaver
is a robust Rust library that provides a flexible and powerful templating engine. Designed for static site generation and more, it offers advanced caching, remote template support, and customizable rendering for optimized performance.
Features
- Flexible Template Rendering: Ideal for static sites, web apps, and other use cases.
- Dynamic Content: Easily interpolate variables in templates with a powerful context system.
- File and String Templates: Render templates from both files and strings.
- Advanced Caching: Improve performance by caching templates for repeated use.
- Custom Rendering: Modify and extend the rendering process to fit your needs.
- Remote Template Support: Fetch and render templates from URLs.
- Comprehensive Error Handling: Gracefully manage template rendering errors.
Installation
Add staticweaver
to your Cargo.toml
:
[dependencies]
staticweaver = "0.0.1"
Usage
Here's a basic example of how to use staticweaver
:
use std::fs;
use staticweaver::engine::Engine;
use staticweaver::EngineError;
use staticweaver::error::TemplateError;
use staticweaver::context::Context;
use std::time::Duration;
use std::path::Path;
fn main() -> Result<(), TemplateError> {
// Create the 'examples' directory and 'template.html' for the test
fs::create_dir_all("examples")?;
fs::write("examples/template.html", r#"
<!DOCTYPE html>
<html>
<head><title>{{title}}</title></head>
<body><h1>{{title}}</h1><p>{{content}}</p></body>
</html>"#)?;
// Create a new engine with a template path and cache duration
let mut engine = Engine::new("examples", Duration::from_secs(60));
// Create a context with some variables
let mut context = Context::new();
context.set("title".to_string(), "Welcome to StaticWeaver".to_string());
context.set("content".to_string(), "This is a simple example.".to_string());
// Render the 'template.html', mapping EngineError to TemplateError
let rendered = engine
.render_page(&context, "template")
.map_err(|e| TemplateError::EngineError(Box::new(EngineError::Render(format!("Rendering failed: {:?}", e)))))?;
// Output the rendered content to 'rendered.html'
fs::write("examples/rendered.html", &rendered)?;
println!("Rendered content written to 'examples/rendered.html'.");
// Clean up: remove files and directory
remove_dir_contents("examples")?;
println!("Template and rendered files cleaned up.");
Ok(())
}
// Helper function to remove all files inside a directory
fn remove_dir_contents(dir: &str) -> std::io::Result<()> {
let path = Path::new(dir);
if path.is_dir() {
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
if entry_path.is_file() {
fs::remove_file(entry_path)?;
} else if entry_path.is_dir() {
fs::remove_dir_all(entry_path)?; // If there are subdirectories, remove them recursively
}
}
fs::remove_dir(path)?; // Finally, remove the directory itself
}
Ok(())
}
This example demonstrates rendering a template named "page", replacing {{title}}
and {{content}}
with values from the context.
Documentation
For full API documentation, please visit docs.rs/staticweaver.
Examples
To explore more examples, clone the repository and run the following command:
cargo run --example example_name
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of
at your option.
Acknowledgements
Special thanks to all contributors who have helped build the staticweaver
library.
Dependencies
~8–21MB
~320K SLoC