diff options
author | mataha <mataha@users.noreply.github.com> | 2023-06-10 04:57:56 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-06-12 10:35:40 +0200 |
commit | cba1fb8e508eae87d213e7bc4c7b2eb8b4b92732 (patch) | |
tree | 36fd43629c3686e2e630db884f5c5379d9c55246 /askama_derive/src | |
parent | fe5d350f50ebc73300456c618dd265cc43a8f05a (diff) | |
download | askama-cba1fb8e508eae87d213e7bc4c7b2eb8b4b92732.tar.gz askama-cba1fb8e508eae87d213e7bc4c7b2eb8b4b92732.tar.bz2 askama-cba1fb8e508eae87d213e7bc4c7b2eb8b4b92732.zip |
Allow macros to be defined and called without arguments
This commit introduces a shorthand for defining and calling macros when
using them as a reusable substitute for variables assigned complex values
(e.g. string literals with or without newline escapes). The use-case is
formatting - from my experience it's easier to visually parse a `macro`
`endmacro` block than a multiline variable assignment.
Signed-off-by: mataha <mataha@users.noreply.github.com>
Diffstat (limited to 'askama_derive/src')
-rw-r--r-- | askama_derive/src/parser/node.rs | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/askama_derive/src/parser/node.rs b/askama_derive/src/parser/node.rs index fc6860e..8c122af 100644 --- a/askama_derive/src/parser/node.rs +++ b/askama_derive/src/parser/node.rs @@ -133,12 +133,13 @@ fn block_call(i: &str) -> IResult<&str, Node<'_>> { cut(tuple(( opt(tuple((ws(identifier), ws(tag("::"))))), ws(identifier), - ws(Expr::parse_arguments), + opt(ws(Expr::parse_arguments)), opt(expr_handle_ws), ))), )); let (i, (pws, _, (scope, name, args, nws))) = p(i)?; let scope = scope.map(|(scope, _)| scope); + let args = args.unwrap_or_default(); Ok((i, Node::Call(Ws(pws, nws), scope, name, args))) } @@ -415,7 +416,7 @@ fn block_macro<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { ws(keyword("macro")), cut(tuple(( ws(identifier), - ws(parameters), + opt(ws(parameters)), opt(expr_handle_ws), |i| tag_block_end(i, s), ))), @@ -435,6 +436,8 @@ fn block_macro<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { assert_ne!(name, "super", "invalid macro name 'super'"); + let params = params.unwrap_or_default(); + Ok(( i, Node::Macro( |