Skip to content

Commit

Permalink
feat(template): add join_path filter (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajpantuso authored Jan 23, 2024
1 parent 651ec02 commit 9341810
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/tera.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::path::Path;
use std::path::{Path, PathBuf};

use once_cell::sync::Lazy;
use tera::{Context, Tera, Value};
Expand Down Expand Up @@ -65,6 +65,18 @@ pub fn get_tera(dir: &Path) -> Tera {
_ => Err("hash input must be a string".into()),
},
);
tera.register_filter(
"join_path",
move |input: &Value, _args: &HashMap<String, Value>| match input {
Value::Array(arr) => arr
.iter()
.map(Value::as_str)
.collect::<Option<PathBuf>>()
.ok_or("join_path input must be an array of strings".into())
.map(|p| Value::String(p.to_string_lossy().to_string())),
_ => Err("join_path input must be an array of strings".into()),
},
);
tera.register_tester(
"file_exists",
move |input: Option<&Value>, _args: &[Value]| match input {
Expand Down

0 comments on commit 9341810

Please sign in to comment.