Browse Source

for loop should be able to loop over values and filtered variables too.

Related, added test to prove that issue #99 is fixed.
Andreas Stenius 11 years ago
parent
commit
d77195d521
2 changed files with 12 additions and 3 deletions
  1. 2 2
      src/erlydtl_parser.yrl
  2. 10 1
      tests/src/erlydtl_unittests.erl

+ 2 - 2
src/erlydtl_parser.yrl

@@ -296,8 +296,8 @@ ForBlock -> ForBraced Elements EmptyBraced Elements EndForBraced : {for, '$1', '
 EmptyBraced -> open_tag empty_keyword close_tag.
 ForBraced -> open_tag for_keyword ForExpression close_tag : '$3'.
 EndForBraced -> open_tag endfor_keyword close_tag.
-ForExpression -> ForGroup in_keyword Variable : {'in', '$1', '$3', false}.
-ForExpression -> ForGroup in_keyword Variable reversed_keyword : {'in', '$1', '$3', true}.
+ForExpression -> ForGroup in_keyword Value : {'in', '$1', '$3', false}.
+ForExpression -> ForGroup in_keyword Value reversed_keyword : {'in', '$1', '$3', true}.
 ForGroup -> identifier : ['$1'].
 ForGroup -> ForGroup ',' identifier : '$1' ++ ['$3'].
 

+ 10 - 1
tests/src/erlydtl_unittests.erl

@@ -279,7 +279,16 @@ tests() ->
 	       <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
 	       [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
 			  [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
-	       <<"1\n\n3\n\n">>}
+	       <<"1\n\n3\n\n">>},
+
+              {"Loop undefined var",
+               <<"{% for i in undef %}i = {{ i }}.\n{% endfor %}">>,
+               [],
+               <<"">>},
+              {"Loop filtered value rather than variable",
+               <<"{% for x in 123|make_list %}{% if not forloop.first %}, {% endif %}{{ x }}{% endfor %}">>,
+               [],
+               <<"1, 2, 3">>}
 	     ]},
      {"for/empty", [
 		    {"Simple loop",