use std::sync::OnceLock;
fn templates() -> &'static tera::Tera {
static T: OnceLock<tera::Tera> = OnceLock::new();
T.get_or_init(|| {
let mut tera = tera::Tera::default();
tera.add_raw_templates([
("base.html", include_str!("templates/base.html")),
("_sidebar.html", include_str!("templates/_sidebar.html")),
("index.html", include_str!("templates/index.html")),
("list.html", include_str!("templates/list.html")),
("detail.html", include_str!("templates/detail.html")),
("form.html", include_str!("templates/form.html")),
("audit_log.html", include_str!("templates/audit_log.html")),
])
.expect("admin templates compile");
tera
})
}
pub(crate) fn render_template(template: &str, ctx: &serde_json::Value) -> String {
let tera_ctx = tera::Context::from_serialize(ctx).expect("admin context serializes");
templates()
.render(template, &tera_ctx)
.expect("admin template renders")
}
pub(crate) fn render_with_chrome(
template: &str,
ctx: &mut serde_json::Value,
chrome: serde_json::Value,
) -> String {
if let (Some(map), Some(extra)) = (ctx.as_object_mut(), chrome.as_object()) {
for (k, v) in extra {
map.entry(k.clone()).or_insert_with(|| v.clone());
}
}
render_template(template, ctx)
}