Skip to content

Commit

Permalink
Allow referencing self as a variable (fixes #207)
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 20, 2019
1 parent c851edb commit 292bf7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ impl<'a> Generator<'a> {
}

fn visit_var(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap {
if self.locals.contains(s) {
if self.locals.contains(s) || s == "self" {
buf.write(s);
} else {
buf.write("self.");
Expand Down
14 changes: 14 additions & 0 deletions testing/tests/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ fn test_let_decl() {
};
assert_eq!(t.render().unwrap(), "bar");
}

#[derive(Template)]
#[template(
source = "{% for v in self.0 %}{{ v }}{% endfor %}",
ext = "txt",
print = "code"
)]
struct SelfIterTemplate(Vec<usize>);

#[test]
fn test_self_iter() {
let t = SelfIterTemplate(vec![1, 2, 3]);
assert_eq!(t.render().unwrap(), "123");
}

0 comments on commit 292bf7b

Please sign in to comment.