use moostache::{FileLoader, LoaderConfig, TemplateLoader};
use std::error::Error;
use serde_json::json;
use indoc::indoc;
#[cfg(windows)]
const TEMPLATES_DIRECTORY: &str = ".\\examples\\templates\\";
#[cfg(not(windows))]
const TEMPLATES_DIRECTORY: &str = "./examples/templates/";
fn main() -> Result<(), Box<dyn Error>> {
let loader = FileLoader::try_from(LoaderConfig {
templates_directory: TEMPLATES_DIRECTORY,
templates_extension: "html",
cache_size: 200,
})?;
let blog = json!({
"title": "John's blog",
"posts": [
{
"title": "Sitting on a bench",
"teaser": "Just sitting on a bench, eating chips..."
},
{
"title": "Walking down the street",
"teaser": "Just walking down the street, looking at birds..."
}
]
});
let rendered = loader.render_to_string("blog", &blog)?;
let expected = indoc! {"
<h1>John's blog</h1>
<h2>Sitting on a bench</h2>
<span>Just sitting on a bench, eating chips...</span>
<h2>Walking down the street</h2>
<span>Just walking down the street, looking at birds...</span>
"};
assert_eq!(rendered.trim(), expected.trim());
Ok(())
}