Crates.io | staticweaver |
lib.rs | staticweaver |
version | 0.0.1 |
source | src |
created_at | 2024-10-17 21:41:08.729352 |
updated_at | 2024-10-17 21:41:08.729352 |
description | A powerful and flexible static site generator and templating engine for Rust. |
homepage | https://staticweaver.com/ |
repository | https://github.com/sebastienrousseau/staticweaver |
max_upload_size | |
id | 1413652 |
size | 206,933 |
StaticWeaver
A fast and flexible templating engine for Rust applications.
• Website • Documentation • Report Bug • Request Feature • Contributing Guidelines
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.
Add staticweaver
to your Cargo.toml
:
[dependencies]
staticweaver = "0.0.1"
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.
For full API documentation, please visit docs.rs/staticweaver.
To explore more examples, clone the repository and run the following command:
cargo run --example example_name
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under either of
at your option.
Special thanks to all contributors who have helped build the staticweaver
library.