3 releases
0.1.2 | Nov 21, 2024 |
---|---|
0.1.1 | Nov 7, 2024 |
0.1.0 | Sep 12, 2020 |
#142 in Template engine
226 downloads per month
Used in 4 crates
(2 directly)
14KB
261 lines
sidoc
Template engine designed to generate indented documents.
lib.rs
:
A library for generating indented scoped text documents consisting of reusable parts.
There are three major steps involved in using idoc:
- Creating document(s)
- Adding document(s) to a render context
- Render output
Simple example
use std::sync::Arc;
use sidoc::{Builder, RenderContext};
fn create_doc() {
// Use a builder to generate document
let mut bldr = Builder::new();
bldr.line("<!DOCTYPE html>");
bldr.scope("<html>", Some("</html>")).exit();
let doc = bldr.build().unwrap();
// Create a render context, add document to it
let mut r = RenderContext::new();
r.doc("root", Arc::new(doc));
// Render the output
let buf = r.render("root").unwrap();
assert_eq!(buf, "<!DOCTYPE html>\n<html>\n</html>\n");
}
Reference example
use std::sync::Arc;
use sidoc::{Builder, RenderContext};
fn create_doc() {
// Use a builder to generate root document
let mut bldr = Builder::new();
bldr.scope("<html>", Some("</html>")).exit();
let doc = bldr.build().unwrap();
// Use a builder to generate sub document
let mut bldr = Builder::new();
bldr.scope("<head>", Some("</head>")).exit();
let subdoc = bldr.build().unwrap();
// Create a render context, add documents to it
let mut r = RenderContext::new();
r.doc("root", Arc::new(doc));
r.doc("head", Arc::new(subdoc));
// Render the output
let buf = r.render("root").unwrap();
assert_eq!(buf, "<html>\n <head>\n </head>\n</html>\n");
}