From 8a25a1ee65031a687ad7d208eaa2843aed6c9752 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 22 Jul 2018 14:59:56 +0100 Subject: Add Template method to return template extension Thanks to Ryan McGrath for the suggestion and initial implementation. --- askama/src/lib.rs | 2 ++ askama_derive/src/generator.rs | 5 +++++ testing/tests/simple.rs | 1 + 3 files changed, 8 insertions(+) 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)] -- cgit