aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-26 16:39:30 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2016-12-26 16:39:30 +0100
commitd82a8fdf39783e3bfae6b2ea9eb08f011ba195b7 (patch)
treede24b605da0a8caab5588bda5d6b94b04873f454
parent1aafcb8a250be2d7880390cc0d3486f8e46b4520 (diff)
downloadaskama-d82a8fdf39783e3bfae6b2ea9eb08f011ba195b7.tar.gz
askama-d82a8fdf39783e3bfae6b2ea9eb08f011ba195b7.tar.bz2
askama-d82a8fdf39783e3bfae6b2ea9eb08f011ba195b7.zip
Support templating for every type implementing Display
-rw-r--r--askama_codegen/src/generator.rs7
-rw-r--r--askama_test/templates/simple.html5
-rw-r--r--askama_test/tests/simple.rs15
3 files changed, 20 insertions, 7 deletions
diff --git a/askama_codegen/src/generator.rs b/askama_codegen/src/generator.rs
index c815676..34c9757 100644
--- a/askama_codegen/src/generator.rs
+++ b/askama_codegen/src/generator.rs
@@ -57,9 +57,10 @@ impl Generator {
}
fn visit_expr(&mut self, s: &[u8]) {
- self.write("buf.push_str(");
- self.write(&format!("&self.{}", str::from_utf8(s).unwrap()));
- self.writeln(");");
+ let var_name = str::from_utf8(s).unwrap();
+ let code = format!("std::fmt::Write::write_fmt(\
+ &mut buf, format_args!(\"{{}}\", self.{})).unwrap();", var_name);
+ self.writeln(&code);
}
fn handle(&mut self, tokens: &Vec<Node>) {
diff --git a/askama_test/templates/simple.html b/askama_test/templates/simple.html
index e544306..51e225b 100644
--- a/askama_test/templates/simple.html
+++ b/askama_test/templates/simple.html
@@ -1 +1,4 @@
-hello world, {{ var }}
+hello world, {{ strvar }}
+with number: {{ num }}
+Iñtërnâtiônàlizætiøn is important
+in vars too: {{ i18n }}
diff --git a/askama_test/tests/simple.rs b/askama_test/tests/simple.rs
index 9dbfc9c..08b4488 100644
--- a/askama_test/tests/simple.rs
+++ b/askama_test/tests/simple.rs
@@ -9,11 +9,20 @@ use askama::Template;
#[derive(Template)]
#[template(path = "simple.html")]
struct TestTemplate {
- var: String,
+ strvar: String,
+ num: i64,
+ i18n: String,
}
#[test]
fn it_works() {
- let s = TestTemplate { var: "foo".to_string() }.render();
- assert_eq!(s, "hello world, foo\n");
+ let s = TestTemplate {
+ strvar: "foo".to_string(),
+ num: 42,
+ i18n: "Iñtërnâtiônàlizætiøn".to_string(),
+ };
+ assert_eq!(s.render(), "hello world, foo\n\
+ with number: 42\n\
+ Iñtërnâtiônàlizætiøn is important\n\
+ in vars too: Iñtërnâtiônàlizætiøn\n");
}