Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support arbitrary number of arguments to ifchanged

  • Loading branch information...
commit 165b1bfebb034a1ff7a3412a588c1f2c1d063901 1 parent 2c62bd0
Evan Miller authored
Showing with 18 additions and 32 deletions.
  1. +17 −18 src/erlydtl_compiler.erl
  2. +1 −14 src/erlydtl_parser.yrl
View
35 src/erlydtl_compiler.erl
@@ -545,24 +545,22 @@ body_ast(DjangoParseTree, Context, TreeWalker) ->
{IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
ifelse_ast(Expression, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
- ({'ifchanged', Expression, Contents}, TreeWalkerAcc) ->
+ ({'ifchanged', '$undefined', Contents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
- case Expression of
- '$undefined' ->
- ifchanged_ast(Contents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
- _ ->
- ifchanged_ast({expr, Expression}, IfAstInfo, ElseAstInfo, Context, TreeWalker2)
- end;
- ({'ifchangedelse', Expression, IfContents, ElseContents}, TreeWalkerAcc) ->
+ ifchanged_contents_ast(Contents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
+ ({'ifchanged', Values, Contents}, TreeWalkerAcc) ->
+ {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
+ {ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
+ ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
+ ({'ifchangedelse', '$undefined', IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
- case Expression of
- '$undefined' ->
- ifchanged_ast(IfContents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
- _ ->
- ifchanged_ast({expr, Expression}, IfAstInfo, ElseAstInfo, Context, TreeWalker2)
- end;
+ ifchanged_contents_ast(IfContents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
+ ({'ifchangedelse', Values, IfContents, ElseContents}, TreeWalkerAcc) ->
+ {IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
+ {ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
+ ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
({'ifelse', Expression, IfContents, ElseContents}, TreeWalkerAcc) ->
{IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
{ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
@@ -1099,19 +1097,20 @@ for_loop_ast(IteratorList, LoopValue, Contents, {EmptyContentsAst, EmptyContents
merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)
}, TreeWalker2}.
-ifchanged_ast({expr, Expressions}, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
+ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
Info = merge_info(IfContentsInfo, ElseContentsInfo),
ValueAstFun = fun(Expr, {LTreeWalker, LInfo, Acc}) ->
{{EAst, EInfo}, ETw} = value_ast(Expr, false, Context, LTreeWalker),
{ETw, merge_info(LInfo, EInfo), [erl_syntax:tuple([erl_syntax:integer(erlang:phash2(Expr)), EAst])|Acc]} end,
- {TreeWalker1, MergedInfo, Changed} = lists:foldl(ValueAstFun, {TreeWalker, Info, []}, Expressions),
+ {TreeWalker1, MergedInfo, Changed} = lists:foldl(ValueAstFun, {TreeWalker, Info, []}, Values),
{{erl_syntax:case_expr(erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(ifchanged), [erl_syntax:list(Changed)]),
[erl_syntax:clause([erl_syntax:atom(true)], none,
[IfContentsAst]),
erl_syntax:clause([erl_syntax:underscore()], none,
[ElseContentsAst])
- ]), MergedInfo}, TreeWalker1};
-ifchanged_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, _Context, TreeWalker) ->
+ ]), MergedInfo}, TreeWalker1}.
+
+ifchanged_contents_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, _Context, TreeWalker) ->
Info = merge_info(IfContentsInfo, ElseContentsInfo),
Key = erl_syntax:integer(erlang:phash2(Contents)),
{{erl_syntax:case_expr(erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(ifchanged), [erl_syntax:list([erl_syntax:tuple([Key, IfContentsAst])])]),
View
15 src/erlydtl_parser.yrl
@@ -85,11 +85,6 @@ Nonterminals
IfChangedBlock
IfChangedBraced
- IfChangedExpression
- IfChangedExpression1
- IfChangedExpression2
- IfChangedExpression3
- IfChangedExpression4
EndIfChangedBraced
IfEqualBlock
@@ -325,15 +320,7 @@ EndIfBraced -> open_tag endif_keyword close_tag.
IfChangedBlock -> IfChangedBraced Elements ElseBraced Elements EndIfChangedBraced : {ifchangedelse, '$1', '$2', '$4'}.
IfChangedBlock -> IfChangedBraced Elements EndIfChangedBraced : {ifchanged, '$1', '$2'}.
IfChangedBraced -> open_tag ifchanged_keyword close_tag.
-IfChangedBraced -> open_tag ifchanged_keyword IfChangedExpression close_tag : '$3'.
-IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 : ['$1', '$2'].
-IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 IfChangedExpression3 : ['$1', '$2', '$3'].
-IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 IfChangedExpression3 IfChangedExpression4: ['$1', '$2', '$3', '$4'].
-IfChangedExpression -> Value : ['$1'].
-IfChangedExpression1 -> Value : '$1'.
-IfChangedExpression2 -> Value : '$1'.
-IfChangedExpression3 -> Value : '$1'.
-IfChangedExpression4 -> Value : '$1'.
+IfChangedBraced -> open_tag ifchanged_keyword Values close_tag : '$3'.
EndIfChangedBraced -> open_tag endifchanged_keyword close_tag.
IfEqualBlock -> IfEqualBraced Elements ElseBraced Elements EndIfEqualBraced : {ifequalelse, '$1', '$2', '$4'}.
Please sign in to comment.
Something went wrong with that request. Please try again.