mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
Add Template method to return template extension
Thanks to Ryan McGrath for the suggestion and initial implementation.
This commit is contained in:
parent
ed98793b50
commit
8a25a1ee65
@ -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::*;
|
||||
|
@ -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("}");
|
||||
}
|
||||
|
||||
|
@ -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)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user