Skip to main content

collect_template_vars

Function collect_template_vars 

Source
pub fn collect_template_vars(template_dir: &Path) -> Result<Vec<String>, Error>
Expand description

Walks template_dir recursively and returns the sorted, deduped set of every {{ <var> }} reference found in the template files.

Filtered references ({{ var | filter }}), dotted paths ({{ a.b }}), helpers ({{#each ...}}), and the {{!...}} raw-emit form are all skipped — only bare top-level keys end up in the result, because those are the ones staticweaver actually looks up against the frontmatter metadata HashMap.

Returns an empty Vec if template_dir doesn’t exist.

§Errors

Returns io::Error only for unexpected failures reading the template tree; a missing directory is treated as “no templates”, not as an error.

§Examples

use ssg::content_stager::collect_template_vars;
use std::fs;

let tmp = tempfile::tempdir().unwrap();
let t = tmp.path().join("t");
fs::create_dir_all(&t).unwrap();
fs::write(t.join("page.html"), "<title>{{ title }}</title>{{ author }}").unwrap();

let vars = collect_template_vars(&t).unwrap();
assert!(vars.contains(&"title".to_string()));
assert!(vars.contains(&"author".to_string()));