Add Template method to return template extension

Thanks to Ryan McGrath for the suggestion and initial implementation.
This commit is contained in:
Dirkjan Ochtman 2018-07-22 14:59:56 +01:00
parent ed98793b50
commit 8a25a1ee65
3 changed files with 8 additions and 0 deletions

View File

@ -337,6 +337,8 @@ pub trait Template {
self.render_into(&mut buf)?;
Ok(buf)
}
/// Helper method to inspect the template's extension
fn extension(&self) -> Option<&str>;
}
pub use askama_derive::*;

View File

@ -117,6 +117,11 @@ impl<'a> Generator<'a> {
self.flush_ws(WS(false, false));
self.writeln("Ok(())");
self.writeln("}");
self.writeln("fn extension(&self) -> Option<&str> {");
self.writeln(&format!("{:?}", self.input.path.extension().map(|s| s.to_str().unwrap())));
self.writeln("}");
self.writeln("}");
}

View File

@ -27,6 +27,7 @@ fn test_variables() {
Iñtërnâtiônàlizætiøn is important\n\
in vars too: Iñtërnâtiônàlizætiøn"
);
assert_eq!(s.extension(), Some("html"));
}
#[derive(Template)]