Skip to content

Commit

Permalink
Allow {% endmacro name %}
Browse files Browse the repository at this point in the history
Just migrated a repo from tera to askama and this was one of the only
things that was different. This is also coherent with `{% block %}` for
which I added the same feature years ago.
  • Loading branch information
Bastien Orivel committed Apr 29, 2022
1 parent c319691 commit 7538354
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
29 changes: 14 additions & 15 deletions askama_shared/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,27 +996,26 @@ fn block_import(i: &str) -> IResult<&str, Node<'_>> {
}

fn block_macro<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
let mut p = tuple((
let mut start = tuple((
opt(expr_handle_ws),
ws(tag("macro")),
cut(tuple((ws(identifier), ws(parameters), opt(expr_handle_ws), |i| {
tag_block_end(i, s)
}))),
));
let (i, (pws1, _, (name, params, nws1, _))) = start(i)?;

let mut end = cut(tuple((
|i| parse_template(i, s),
cut(tuple((
ws(identifier),
ws(parameters),
|i| tag_block_start(i, s),
opt(expr_handle_ws),
|i| tag_block_end(i, s),
cut(tuple((
|i| parse_template(i, s),
cut(tuple((
|i| tag_block_start(i, s),
opt(expr_handle_ws),
ws(tag("endmacro")),
opt(expr_handle_ws),
))),
))),
ws(tag("endmacro")),
cut(tuple((opt(ws(tag(name))), opt(expr_handle_ws)))),
))),
));
)));
let (i, (contents, (_, pws2, _, (_, nws2)))) = end(i)?;

let (i, (pws1, _, (name, params, nws1, _, (contents, (_, pws2, _, nws2))))) = p(i)?;
assert_ne!(name, "super", "invalid macro name 'super'");

Ok((
Expand Down
12 changes: 12 additions & 0 deletions testing/templates/macro.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@
{%- call thrice(s) -%}

3

{%- macro twice(param) -%}

{{ param }} {{ param }}

{%- endmacro twice -%}

4

{%- call twice(s) -%}

5
2 changes: 1 addition & 1 deletion testing/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct MacroTemplate<'a> {
#[test]
fn test_macro() {
let t = MacroTemplate { s: "foo" };
assert_eq!(t.render().unwrap(), "12foo foo foo3");
assert_eq!(t.render().unwrap(), "12foo foo foo34foo foo5");
}

#[derive(Template)]
Expand Down

0 comments on commit 7538354

Please sign in to comment.