diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-22 14:59:56 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-22 14:59:56 +0100 |
commit | 8a25a1ee65031a687ad7d208eaa2843aed6c9752 (patch) | |
tree | 83e16e2662a3182ea94989d436e9752d2ca54bdb | |
parent | ed98793b506c66723c713991f40c0065557a35d5 (diff) | |
download | askama-8a25a1ee65031a687ad7d208eaa2843aed6c9752.tar.gz askama-8a25a1ee65031a687ad7d208eaa2843aed6c9752.tar.bz2 askama-8a25a1ee65031a687ad7d208eaa2843aed6c9752.zip |
Add Template method to return template extension
Thanks to Ryan McGrath for the suggestion and initial implementation.
Diffstat (limited to '')
-rw-r--r-- | askama/src/lib.rs | 2 | ||||
-rw-r--r-- | askama_derive/src/generator.rs | 5 | ||||
-rw-r--r-- | testing/tests/simple.rs | 1 |
3 files changed, 8 insertions, 0 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 77e49f2..c7cd182 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -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::*; diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 5af38c0..db9ac17 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -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("}"); } diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 95c14b1..4af2cea 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -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)] |