aboutsummaryrefslogtreecommitdiffstats
path: root/testing
diff options
context:
space:
mode:
authorLibravatar Anthony Nowell <anthony@algorithmia.com>2017-08-13 00:46:04 -0600
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-23 12:40:14 +0200
commit2d6785b71414cc70adfb24ee742e3defa5841bbb (patch)
tree76d691a26e266b98f21481104155c523f5e62693 /testing
parentd0a3d51dcda1f4c0509f9e083e3d02c81d23776a (diff)
downloadaskama-2d6785b71414cc70adfb24ee742e3defa5841bbb.tar.gz
askama-2d6785b71414cc70adfb24ee742e3defa5841bbb.tar.bz2
askama-2d6785b71414cc70adfb24ee742e3defa5841bbb.zip
Adding optional json filter
Diffstat (limited to 'testing')
-rw-r--r--testing/Cargo.toml5
-rw-r--r--testing/templates/json.html5
-rw-r--r--testing/tests/simple.rs22
3 files changed, 25 insertions, 7 deletions
diff --git a/testing/Cargo.toml b/testing/Cargo.toml
index bb6b534..be9af26 100644
--- a/testing/Cargo.toml
+++ b/testing/Cargo.toml
@@ -6,7 +6,8 @@ workspace = ".."
build = "build.rs"
[dependencies]
-askama = { path = "../askama", version = "*" }
+serde_json = "1.0"
+askama = { path = "../askama", version = "*", features = ["serde-json"] }
[build-dependencies]
-askama = { path = "../askama", version = "*" }
+askama = { path = "../askama", version = "*", features = ["serde-json"] }
diff --git a/testing/templates/json.html b/testing/templates/json.html
index e711c10..250b7be 100644
--- a/testing/templates/json.html
+++ b/testing/templates/json.html
@@ -1 +1,4 @@
-{"foo": "{{ foo }}", "bar": "{{ bar }}"}
+{
+ "foo": "{{ foo }}",
+ "bar": {{ bar|json }}
+}
diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs
index 896e082..93faf50 100644
--- a/testing/tests/simple.rs
+++ b/testing/tests/simple.rs
@@ -1,7 +1,10 @@
#[macro_use]
extern crate askama;
+#[macro_use]
+extern crate serde_json;
use askama::Template;
+use serde_json::Value;
#[derive(Template)]
#[template(path = "simple.html")]
@@ -146,16 +149,27 @@ fn test_generics() {
#[template(path = "json.html")]
struct JsonTemplate<'a> {
foo: &'a str,
- bar: &'a str,
+ bar: &'a Value,
}
#[test]
fn test_json() {
- let t = JsonTemplate { foo: "a", bar: "b" };
- assert_eq!(t.render().unwrap(), "{\"foo\": \"a\", \"bar\": \"b\"}");
+ let val = json!({"arr": [ "one", 2, true, null ]});
+ let t = JsonTemplate { foo: "a", bar: &val };
+ // Note: the json filter lacks a way to specify initial indentation
+ assert_eq!(t.render().unwrap(), r#"{
+ "foo": "a",
+ "bar": {
+ "arr": [
+ "one",
+ 2,
+ true,
+ null
+ ]
+}
+}"#);
}
-
#[derive(Template)]
#[template(path = "composition.html")]
struct CompositionTemplate {