From 1442aa71e5d0cb408bcd360f39e35deb2e734746 Mon Sep 17 00:00:00 2001 From: Jordan Danford Date: Thu, 9 Mar 2017 09:49:45 -0700 Subject: Fix formatting in "Variables" section of docs --- askama/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 311992f..1427854 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -51,8 +51,8 @@ //! Reading from variables is subject to the usual borrowing policies. //! For example, `{{ name }}` will get the ``name`` field from the template //! context, -//! while `{{ user.name }}` will get the ``name`` field of the `user` -//! ``field`` of the template context. +//! while `{{ user.name }}` will get the ``name`` field of the ``user`` +//! field of the template context. //! //! ## Filters //! -- cgit From 24fdb49e30895339470364725a76e7f785809b39 Mon Sep 17 00:00:00 2001 From: Jordan Danford Date: Thu, 9 Mar 2017 10:41:49 -0700 Subject: Change preposition in "Variables" section of docs Changed "of the template context" to "to the template context" --- askama/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 1427854..e70194b 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -52,7 +52,7 @@ //! For example, `{{ name }}` will get the ``name`` field from the template //! context, //! while `{{ user.name }}` will get the ``name`` field of the ``user`` -//! field of the template context. +//! field from the template context. //! //! ## Filters //! -- cgit From ba70b11f40b960bf09d2a43e106bdad196cc158b Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Fri, 10 Mar 2017 12:54:12 -0800 Subject: Bump versions in README Without using this version I was getting a warning on stable rust. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a346348..fe9bb12 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,10 @@ First, add the following to your crate's `Cargo.toml`: build = "build.rs" # in section [dependencies] -askama = "0.1" -askama_derive = "0.1" +askama = "0.3" # in section [build-dependencies] -askama = "0.1" +askama = "0.3" ``` Because Askama will generate Rust code from your template files, -- cgit From 7362ca0dcb61ec70ad88f74873f459702c5c0dde Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Fri, 10 Mar 2017 13:32:30 -0800 Subject: Add `lower` and `upper` filters --- askama/src/filters.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/askama/src/filters.rs b/askama/src/filters.rs index 3f6aede..bd64bb0 100644 --- a/askama/src/filters.rs +++ b/askama/src/filters.rs @@ -57,6 +57,28 @@ pub fn e(s: &fmt::Display) -> String { /// the Askama code generator. pub fn format() { } +/// Converts to lowercase. +pub fn lower(s: &fmt::Display) -> String { + let s = format!("{}", s); + s.to_lowercase() +} + +/// Alias for the `lower()` filter. +pub fn lowercase(s: &fmt::Display) -> String { + lower(s) +} + +/// Converts to uppercase. +pub fn upper(s: &fmt::Display) -> String { + let s = format!("{}", s); + s.to_uppercase() +} + +/// Alias for the `upper()` filter. +pub fn uppercase(s: &fmt::Display) -> String { + upper(s) +} + #[cfg(test)] mod tests { use super::*; @@ -66,5 +88,15 @@ mod tests { assert_eq!(escape(&"<&>"), "<&>"); assert_eq!(escape(&"bla&"), "bla&"); assert_eq!(escape(&" Date: Sat, 11 Mar 2017 21:23:02 +0700 Subject: added trim filter (#18) --- askama/src/filters.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/askama/src/filters.rs b/askama/src/filters.rs index bd64bb0..62f2616 100644 --- a/askama/src/filters.rs +++ b/askama/src/filters.rs @@ -79,6 +79,12 @@ pub fn uppercase(s: &fmt::Display) -> String { upper(s) } +/// Strip leading and trailing whitespace. +pub fn trim(s: &fmt::Display) -> String { + let s = format!("{}", s); + s.trim().to_owned() +} + #[cfg(test)] mod tests { use super::*; @@ -99,4 +105,9 @@ mod tests { assert_eq!(upper(&"FooBar"), "FOOBAR"); assert_eq!(upper(&"foo"), "FOO"); } + + #[test] + fn test_trim() { + assert_eq!(trim(&" Hello\tworld\t"), "Hello\tworld"); + } } -- cgit From bedc16cbe3c6938180f53ddfc781333a2ece52ee Mon Sep 17 00:00:00 2001 From: Andy Librian Date: Sat, 11 Mar 2017 21:24:21 +0700 Subject: reorganized filter tests --- askama/src/filters.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/askama/src/filters.rs b/askama/src/filters.rs index 62f2616..ccbf1a0 100644 --- a/askama/src/filters.rs +++ b/askama/src/filters.rs @@ -94,12 +94,18 @@ mod tests { assert_eq!(escape(&"<&>"), "<&>"); assert_eq!(escape(&"bla&"), "bla&"); assert_eq!(escape(&" Date: Sat, 11 Mar 2017 22:24:00 +0100 Subject: Reference askama and std from the global scope (fixes #17) With this change, importing `std` and `askama` into modules where templates are defined is no longer necessary. `askama::Template` must still be imported in scopes where methods of the `Template` trait are called. --- askama_derive/src/generator.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index edde3fe..f055cb7 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -129,7 +129,7 @@ impl<'a> Generator<'a> { if name == "format" { self.write("format!("); } else { - self.write(&format!("askama::filters::{}(&", name)); + self.write(&format!("::askama::filters::{}(&", name)); } for (i, arg) in args.iter().enumerate() { @@ -274,7 +274,7 @@ impl<'a> Generator<'a> { ws2: &WS) { self.writeln("#[allow(unused_variables)]"); self.writeln(&format!( - "fn render_block_{}_to(&self, writer: &mut std::fmt::Write) {{", + "fn render_block_{}_to(&self, writer: &mut ::std::fmt::Write) {{", name)); self.prepare_ws(ws1); self.handle(nodes); @@ -351,7 +351,7 @@ impl<'a> Generator<'a> { let name = if let Some(suffix) = trait_suffix { format!("TraitFrom{}", suffix) } else { - "askama::Template".to_string() + "::askama::Template".to_string() }; self.writeln(&format!("impl{} {} for {}{}{} {{", full_anno.as_str(), &name, ast.ident.as_ref(), @@ -360,7 +360,7 @@ impl<'a> Generator<'a> { fn impl_template(&mut self, ast: &syn::DeriveInput, nodes: &'a [Node]) { self.write_header(ast, None); - self.writeln("fn render_to(&self, writer: &mut std::fmt::Write) {"); + self.writeln("fn render_to(&self, writer: &mut ::std::fmt::Write) {"); self.handle(nodes); self.flush_ws(&WS(false, false)); self.writeln("}"); @@ -375,7 +375,7 @@ impl<'a> Generator<'a> { self.writeln("#[allow(unused_variables)]"); let trait_name = format!("TraitFrom{}", path_as_identifier(base)); self.writeln(&format!( - "fn render_trait_to(&self, timpl: &{}, writer: &mut std::fmt::Write) {{", + "fn render_trait_to(&self, timpl: &{}, writer: &mut ::std::fmt::Write) {{", trait_name)); if let Some(nodes) = nodes { @@ -392,7 +392,7 @@ impl<'a> Generator<'a> { fn impl_template_for_trait(&mut self, ast: &syn::DeriveInput, derived: bool) { self.write_header(ast, None); - self.writeln("fn render_to(&self, writer: &mut std::fmt::Write) {"); + self.writeln("fn render_to(&self, writer: &mut ::std::fmt::Write) {"); if derived { self.writeln("self._parent.render_trait_to(self, writer);"); } else { @@ -408,11 +408,11 @@ impl<'a> Generator<'a> { for bname in block_names { self.writeln(&format!( - "fn render_block_{}_to(&self, writer: &mut std::fmt::Write);", + "fn render_block_{}_to(&self, writer: &mut ::std::fmt::Write);", bname)); } self.writeln(&format!( - "fn render_trait_to(&self, timpl: &{}, writer: &mut std::fmt::Write);", + "fn render_trait_to(&self, timpl: &{}, writer: &mut ::std::fmt::Write);", trait_name)); self.writeln("}"); -- cgit From 2585a4dae0dc88b9814c0c06c9489e2b86a1aa6d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 12 Mar 2017 20:53:13 +0100 Subject: Add some documentation about inheritance pitfalls --- askama/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/askama/src/lib.rs b/askama/src/lib.rs index e70194b..9126a99 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -105,6 +105,8 @@ //! //! The `block` tags define three blocks that can be filled in by child //! templates. The base template defines a default version of the block. +//! A base template must define one or more blocks in order to be enable +//! inheritance. //! //! ### Child template //! @@ -133,6 +135,11 @@ //! have a field called `_parent` of the type used as the base template //! context. Blocks can only refer to the context of their own template. //! +//! Note that, if the base template lives in another module than the child +//! template, the child template's module should import all symbols from the +//! base template's module in order for it to find the trait definition that +//! supports the inheritance mechanism. +//! //! ## HTML escaping //! //! Askama does not yet support automatic escaping. Care must be taken to -- cgit From 1d798e4a489709e3fa1603d8af9fb3c48de0d87e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 12 Mar 2017 20:55:15 +0100 Subject: Bump version to 0.3.2 --- Cargo.lock | 8 ++++---- askama/Cargo.toml | 4 ++-- askama_derive/Cargo.toml | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3682913..e6b290e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,19 +2,19 @@ name = "askama_testing" version = "0.1.0" dependencies = [ - "askama 0.3.1", + "askama 0.3.2", ] [[package]] name = "askama" -version = "0.3.1" +version = "0.3.2" dependencies = [ - "askama_derive 0.3.1", + "askama_derive 0.3.2", ] [[package]] name = "askama_derive" -version = "0.3.1" +version = "0.3.2" dependencies = [ "nom 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/askama/Cargo.toml b/askama/Cargo.toml index 178d8c9..fe29473 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "askama" -version = "0.3.1" +version = "0.3.2" authors = ["Dirkjan Ochtman "] description = "Type-safe, compiled Jinja-like templates for Rust" documentation = "https://docs.rs/askama" @@ -16,4 +16,4 @@ readme = "../README.md" travis-ci = { repository = "djc/askama" } [dependencies] -askama_derive = { path = "../askama_derive", version = "0.3.1" } +askama_derive = { path = "../askama_derive", version = "0.3.2" } diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 3f17f40..885b11a 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "askama_derive" -version = "0.3.1" +version = "0.3.2" authors = ["Dirkjan Ochtman "] description = "Procedural macro package for Askama" homepage = "https://github.com/djc/askama" -- cgit From f0efc21b3ec37160c7202b3613f68325d70b5e64 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 12 May 2017 20:31:31 +0200 Subject: Update dependencies --- Cargo.lock | 12 ++++++------ askama_derive/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6b290e..0348fc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,14 +16,14 @@ dependencies = [ name = "askama_derive" version = "0.3.2" dependencies = [ - "nom 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.11.9 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "nom" -version = "2.1.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -33,7 +33,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "syn" -version = "0.11.9" +version = "0.11.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -55,8 +55,8 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum nom 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e5d4598834859fedb9a0a69d5b862a970e77982a92f544d547257a4d49469067" +"checksum nom 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5388009f277d5d889ee01f0ba92e3620c759402cdf52a8d40e9ac06969ee3c88" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" -"checksum syn 0.11.9 (registry+https://github.com/rust-lang/crates.io-index)" = "480c834701caba3548aa991e54677281be3a5414a9d09ddbdf4ed74a569a9d19" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 885b11a..f916d85 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -12,6 +12,6 @@ workspace = ".." proc-macro = true [dependencies] -nom = "2.1" +nom = "3" syn = "0.11" quote = "0.3" -- cgit From 52733424e789757037539a6f4a8cf5129eeb6655 Mon Sep 17 00:00:00 2001 From: Jordan Danford Date: Sat, 13 May 2017 16:32:38 -0700 Subject: Remove duplicate "if/else" --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe9bb12..f9b7286 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Many thanks to [David Tolnay][dtolnay] for his support in improving Askama. ### Supported in templates * Template inheritance (one level only) -* Basic loops and if/else if/else statements +* Basic loops and if/else statements * Whitespace suppressing with '-' markers * Some built-in filters -- cgit From 71d99927a63bc406023484ab21f885cd985c9a88 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 26 May 2017 11:45:01 +0200 Subject: Add support for chained attributes (fixes #22) --- askama_derive/src/parser.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index ce13add..9a9c89e 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -141,18 +141,27 @@ named!(expr_single, alt!( expr_group )); -named!(expr_attr, alt!( - do_parse!( - obj: expr_single >> - tag_s!(".") >> - attr: identifier >> - args: arguments >> - (if args.is_some() { - Expr::Call(Box::new(obj), attr, args.unwrap()) - } else { - Expr::Attr(Box::new(obj), attr) - }) - ) | expr_single +named!(attr<(&str, Option>)>, do_parse!( + tag_s!(".") >> + attr: identifier >> + args: arguments >> + (attr, args) +)); + +named!(expr_attr, do_parse!( + obj: expr_single >> + attrs: many0!(attr) >> + ({ + let mut res = obj; + for (aname, args) in attrs { + res = if args.is_some() { + Expr::Call(Box::new(res), aname, args.unwrap()) + } else { + Expr::Attr(Box::new(res), aname) + }; + } + res + }) )); named!(filter<(&str, Option>)>, do_parse!( -- cgit From ce84543b69b69ab2030ce4daeae3f26d5008b11d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 26 May 2017 11:45:18 +0200 Subject: Add test for basic chained attributes --- testing/templates/nested-attr.html | 1 + testing/tests/simple.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 testing/templates/nested-attr.html diff --git a/testing/templates/nested-attr.html b/testing/templates/nested-attr.html new file mode 100644 index 0000000..3bbbbc3 --- /dev/null +++ b/testing/templates/nested-attr.html @@ -0,0 +1 @@ +{{ inner.holder.a }} diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index a04b3fa..980321f 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -93,6 +93,25 @@ fn test_attr() { } +struct NestedHolder { + holder: Holder, +} + +#[derive(Template)] +#[template(path = "nested-attr.html")] +struct NestedAttrTemplate { + inner: NestedHolder, +} + +#[test] +fn test_nested_attr() { + let t = NestedAttrTemplate { + inner: NestedHolder { holder: Holder { a: 5 } } + }; + assert_eq!(t.render(), "5"); +} + + #[derive(Template)] #[template(path = "option.html")] struct OptionTemplate<'a> { -- cgit