mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 07:20:55 +00:00
Merge pull request #38 from GuillaumeGomez/prevent-macro-move-var
Prevent jinja macros to move variables
This commit is contained in:
commit
223470eb20
@ -687,6 +687,9 @@ impl<'a> Generator<'a> {
|
|||||||
names.write(arg);
|
names.write(arg);
|
||||||
|
|
||||||
values.write("(");
|
values.write("(");
|
||||||
|
if !is_copyable(expr) {
|
||||||
|
values.write("&");
|
||||||
|
}
|
||||||
values.write(self.visit_expr_root(ctx, expr)?);
|
values.write(self.visit_expr_root(ctx, expr)?);
|
||||||
values.write(")");
|
values.write(")");
|
||||||
self.locals.insert_with_default(Cow::Borrowed(arg));
|
self.locals.insert_with_default(Cow::Borrowed(arg));
|
||||||
|
@ -99,3 +99,31 @@ fn test_double_attr_arg() {
|
|||||||
let t = DoubleAttrArg { x: (10,) };
|
let t = DoubleAttrArg { x: (10,) };
|
||||||
assert_eq!(t.render().unwrap(), "156");
|
assert_eq!(t.render().unwrap(), "156");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensures that fields are not moved when calling a jinja macro.
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(
|
||||||
|
source = "
|
||||||
|
{%- macro package_navigation(title, show) -%}
|
||||||
|
{%- if show -%}
|
||||||
|
{{title}}
|
||||||
|
{%- else -%}
|
||||||
|
no show
|
||||||
|
{%- endif -%}
|
||||||
|
{%- endmacro -%}
|
||||||
|
|
||||||
|
{%- call package_navigation(title=title, show=true) -%}
|
||||||
|
",
|
||||||
|
ext = "html"
|
||||||
|
)]
|
||||||
|
struct DoNotMoveFields {
|
||||||
|
title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_do_not_move_fields() {
|
||||||
|
let x = DoNotMoveFields {
|
||||||
|
title: "a".to_string(),
|
||||||
|
};
|
||||||
|
assert_eq!(x.render().unwrap(), "a");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user