July 2026

Send Global Data to a MiniJinja Function

There are just scratch notes

use crate::config::Config;
use crate::filters::*;
use crate::functions::*;
use anyhow::Result;
use minijinja::syntax::SyntaxConfig;
use minijinja::{Environment, Value, path_loader};
use std::path::PathBuf;

pub fn get_env(
  dir: &str,
  config: Config,
) -> Result<Environment<'static>> {
  let mut env = Environment::new();
  // other set up stuff here
  let exists_config = config.clone();
  env.add_function("exists", move |path: Value| -> bool {
    exists(path, exists_config.content_dir())
  });
  Ok(env)
}

pub fn exists(
  path: Value,
  root: PathBuf,
) -> bool {
  let rel_path =
    path.to_string().trim_start_matches("/").to_string();
  root.join(rel_path).exists()
}

Call like:

[@ exists("/some/path/index.html") @]

It's look in the content directory from the config like:

content/some/path/index.html

end of line