mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-30 06:21:13 +00:00
Implement Display for all Templates
This commit is contained in:
parent
1e0ee705a8
commit
cece25b0be
@ -47,6 +47,7 @@ pub fn generate(ast: &syn::DeriveInput, path: &str, mut nodes: Vec<Node>) -> Str
|
||||
} else {
|
||||
gen.impl_template(ast, &content);
|
||||
}
|
||||
gen.impl_display(ast);
|
||||
gen.result()
|
||||
}
|
||||
|
||||
@ -490,6 +491,15 @@ impl<'a> Generator<'a> {
|
||||
self.writeln("}");
|
||||
}
|
||||
|
||||
// Implement `Display` for the given context struct.
|
||||
fn impl_display(&mut self, ast: &syn::DeriveInput) {
|
||||
self.write_header(ast, "::std::fmt::Display");
|
||||
self.writeln("fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {");
|
||||
self.writeln("self.render_into(f)");
|
||||
self.writeln("}");
|
||||
self.writeln("}");
|
||||
}
|
||||
|
||||
// Implement `Deref<Parent>` for an inheriting context struct.
|
||||
fn deref_to_parent(&mut self, ast: &syn::DeriveInput, parent_type: &syn::Ty) {
|
||||
self.write_header(ast, "::std::ops::Deref");
|
||||
|
@ -154,3 +154,16 @@ fn test_json() {
|
||||
let t = JsonTemplate { foo: "a", bar: "b" };
|
||||
assert_eq!(t.render().unwrap(), "{\"foo\": \"a\", \"bar\": \"b\"}");
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "composition.html")]
|
||||
struct CompositionTemplate {
|
||||
foo: IfTemplate,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_composition() {
|
||||
let t = CompositionTemplate { foo: IfTemplate { cond: true } };
|
||||
assert_eq!(t.render().unwrap(), "composed: true");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user