aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-06 14:42:24 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-06 14:44:29 +0200
commit670dbe4aecf4280ef254cafa7d97d1890946b3e5 (patch)
tree432722b34f96e436d7e3517c0be9a5531182a863
parente293a1a90c10f33f1fb147cc2fc4023ad43335f6 (diff)
downloadaskama-670dbe4aecf4280ef254cafa7d97d1890946b3e5.tar.gz
askama-670dbe4aecf4280ef254cafa7d97d1890946b3e5.tar.bz2
askama-670dbe4aecf4280ef254cafa7d97d1890946b3e5.zip
Add function to find template relative to another template's path
-rw-r--r--askama_derive/src/path.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/askama_derive/src/path.rs b/askama_derive/src/path.rs
index aac6557..728c7ec 100644
--- a/askama_derive/src/path.rs
+++ b/askama_derive/src/path.rs
@@ -9,6 +9,30 @@ fn template_dir() -> PathBuf {
path
}
+pub fn find_template_from_path<'a>(path: &str, start_at: Option<&str>) -> PathBuf {
+ let root = template_dir();
+ match start_at {
+ Some(rel) => {
+ let mut fs_rel_path = root.clone();
+ fs_rel_path.push(rel);
+ fs_rel_path = fs_rel_path.with_file_name(path);
+ if fs_rel_path.exists() {
+ return fs_rel_path.strip_prefix(&root).unwrap().to_owned();
+ }
+ },
+ None => {},
+ }
+
+ let mut fs_abs_path = root.clone();
+ let path = Path::new(path);
+ fs_abs_path.push(Path::new(path));
+ if fs_abs_path.exists() {
+ path.to_owned()
+ } else {
+ panic!(format!("template '{:?}' not found", path.to_str()));
+ }
+}
+
pub fn get_template_source(tpl_file: &str) -> String {
let mut path = template_dir();
path.push(Path::new(tpl_file));