chiark / gitweb /
web templates: Provide our own directory scanner
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 28 Mar 2022 00:07:37 +0000 (01:07 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 28 Mar 2022 00:15:28 +0000 (01:15 +0100)
We don't know exactly what Tera::new() does, but in any case we want
to do something it doesn't support: have template names which do not
contain the .tera file extension.

No functional change in this commit.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/main.rs

index 63e9340263fbd8434da8dff23662f3f0a8cab7fd..bd2b6828b3d7d429486a67034e62a40c931cb28f 100644 (file)
@@ -69,8 +69,19 @@ pub struct Templates {
 impl Templates {
   #[throws(StartupError)]
   pub fn new(template_dir: &str) -> Self {
-    let tera = Tera::new(&format!("{}/*.tera", template_dir))
-    .context("initialise templates")?;
+    let mut tera = Tera::default();
+    (||{
+      let files: Vec<_> = 
+        fs::read_dir(template_dir).context("open directory")?
+        .filter_map_ok(|entry| {
+          let leaf = entry.file_name().into_string().ok()?;
+          leaf.ends_with(".tera").then(||())?;
+          Some((entry.path(), Some(leaf)))
+        })
+        .try_collect().context("read directory")?;
+      tera.add_template_files(files).context("process templates")?;
+      Ok::<_,StartupError>(())
+    })().with_context(|| format!("{:?}", template_dir))?;
     Templates { tera }
   }