use std::path::Path;
pub use tera::Context;
use crate::{Error, Result};
pub fn render_str(template: &str, context: &Context) -> Result<String> {
tera::Tera::one_off(template, context, false).map_err(|e| Error::Template(e.to_string()))
}
pub fn render_file(path: &Path, context: &Context) -> Result<String> {
let template = std::fs::read_to_string(path)?;
render_str(&template, context)
}
pub(crate) fn importmap_context(importmap: &crate::importmap::Importmap) -> Context {
let mut ctx = Context::new();
ctx.insert("importmap", &importmap.to_script_tag());
ctx
}
#[cfg(feature = "cli")]
crate::cli_config::feature_args!(
TeraArgs,
tera,
"tera",
no_tera,
"no-tera",
crate::cli_config::NoConfig
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn renders_and_passes_html_through_unescaped() {
let mut ctx = Context::new();
ctx.insert("title", "BMF");
ctx.insert("importmap", "<script type=\"importmap\">{}</script>");
let html = render_str("<title>{{ title }}</title>{{ importmap | safe }}", &ctx).unwrap();
assert!(html.contains("<title>BMF</title>"));
assert!(html.contains("<script type=\"importmap\">"));
}
}