From 42c4cb1d877d696bb97aa2e9402429dad42d7d66 Mon Sep 17 00:00:00 2001 From: Andrew Dona-Couch Date: Wed, 8 Jul 2020 17:03:02 +0000 Subject: Add fmt filter that swaps the first two arguments to format!(). This allows a more natural filter usage: `{{ val | fmt("{:?}") }}` as well as enabling convenient filter composition: `{{ price | to_f64 | fmt("${:.2}") | center }}` --- testing/tests/filters.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'testing') diff --git a/testing/tests/filters.rs b/testing/tests/filters.rs index b02a64b..035e5da 100644 --- a/testing/tests/filters.rs +++ b/testing/tests/filters.rs @@ -38,6 +38,18 @@ fn filter_format() { assert_eq!(t.render().unwrap(), "\"formatted\""); } +#[derive(Template)] +#[template(source = "{{ var|fmt(\"{:?}\") }}", ext = "html", escape = "none")] +struct FmtTemplate<'a> { + var: &'a str, +} + +#[test] +fn filter_fmt() { + let t = FmtTemplate { var: "formatted" }; + assert_eq!(t.render().unwrap(), "\"formatted\""); +} + #[derive(Template)] #[template(source = "{{ s|myfilter }}", ext = "txt")] struct MyFilterTemplate<'a> { -- cgit