Browse Source

Merge pull request #35 from psyeugenic/egil/support-elif

Support {% elif %} clause in if template tag syntax
Evan Miller 13 years ago
parent
commit
a61e741d89
4 changed files with 23 additions and 2 deletions
  1. 4 0
      src/erlydtl_compiler.erl
  2. 11 1
      src/erlydtl_parser.yrl
  3. 1 1
      src/erlydtl_scanner.erl
  4. 7 0
      tests/src/erlydtl_unittests.erl

+ 4 - 0
src/erlydtl_compiler.erl

@@ -541,6 +541,10 @@ body_ast(DjangoParseTree, Context, TreeWalker) ->
             ({'for', {'in', IteratorList, Variable}, Contents, EmptyPartContents}, TreeWalkerAcc) ->
                 {EmptyAstInfo, TreeWalker1} = body_ast(EmptyPartContents, Context, TreeWalkerAcc),
                 for_loop_ast(IteratorList, Variable, Contents, EmptyAstInfo, Context, TreeWalker1);
+            ({'if', Expression, Contents, Elif}, TreeWalkerAcc) ->
+                {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
+                {ElifAstInfo, TreeWalker2} = body_ast(Elif, Context, TreeWalker1),
+                ifelse_ast(Expression, IfAstInfo, ElifAstInfo, Context, TreeWalker2);
             ({'if', Expression, Contents}, TreeWalkerAcc) ->
                 {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
                 {ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),

+ 11 - 1
src/erlydtl_parser.yrl

@@ -79,6 +79,8 @@ Nonterminals
 
     IfBlock
     IfBraced
+    ElifBlock
+    ElifBraced
     IfExpression
     ElseBraced
     EndIfBraced
@@ -137,6 +139,7 @@ Terminals
     close_var
     comment_keyword
     cycle_keyword
+    elif_keyword
     else_keyword
     empty_keyword
     endautoescape_keyword
@@ -296,9 +299,14 @@ ForExpression -> ForGroup in_keyword Variable : {'in', '$1', '$3'}.
 ForGroup -> identifier : ['$1'].
 ForGroup -> ForGroup ',' identifier : '$1' ++ ['$3'].
 
-IfBlock -> IfBraced Elements ElseBraced Elements EndIfBraced : {ifelse, '$1', '$2', '$4'}.
+IfBlock -> IfBraced Elements ElseBraced Elements EndIfBraced : {'ifelse', '$1', '$2', '$4'}.
 IfBlock -> IfBraced Elements EndIfBraced : {'if', '$1', '$2'}.
+IfBlock -> IfBraced Elements ElifBlock : {'if', '$1', '$2', ['$3']}.
+ElifBlock -> ElifBraced Elements ElseBraced Elements EndIfBraced : {'ifelse', '$1', '$2', '$4'}.
+ElifBlock -> ElifBraced Elements EndIfBraced : {'if', '$1', '$2'}.
+ElifBlock -> ElifBraced Elements ElifBlock : {'if', '$1', '$2', ['$3']}.
 IfBraced -> open_tag if_keyword IfExpression close_tag : '$3'.
+ElifBraced -> open_tag elif_keyword IfExpression close_tag : '$3'.
 IfExpression -> Value in_keyword Value : {'expr', "in", '$1', '$3'}.
 IfExpression -> Value not_keyword in_keyword Value : {'expr', "not", {'expr', "in", '$1', '$4'}}.
 IfExpression -> Value '==' Value : {'expr', "eq", '$1', '$3'}.
@@ -384,3 +392,5 @@ Args -> Args identifier '=' Value : '$1' ++ [{'$2', '$4'}].
 
 CallTag -> open_tag call_keyword identifier close_tag : {call, '$3'}.
 CallWithTag -> open_tag call_keyword identifier with_keyword Value close_tag : {call, '$3', '$5'}.
+
+%% vim: syntax=erlang

+ 1 - 1
src/erlydtl_scanner.erl

@@ -76,7 +76,7 @@ scan([], Scanned, _, in_text) ->
 
                             "for", "in", "empty", "endfor", 
 
-                            "if", "else", "endif", "not", "or", "and", 
+                            "if", "elif", "else", "endif", "not", "or", "and", 
 
                             "ifchanged", "endifchanged",
                             

+ 7 - 0
tests/src/erlydtl_unittests.erl

@@ -87,6 +87,13 @@ tests() ->
         {"if", [
                 {"If/else",
                     <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
+                {"If elif",
+		    <<"{% if var1 %}boo{% elif var2 %}yay{% endif %}">>, [{var1, ""}, {var2, "happy"}], <<"yay">>},
+                {"If elif/else",
+		    <<"{% if var1 %}boo{% elif var2 %}sad{% else %}yay{% endif %}">>, [{var1, ""}, {var2, ""}], <<"yay">>},
+                {"If elif/elif/else",
+		    <<"{% if var1 %}boo{% elif var2 %}yay{% elif var3 %}sad{% else %}noo{% endif %}">>, [{var1, ""},
+			{var2, "happy"}, {var3, "not_taken"}], <<"yay">>},
                 {"If",
                     <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
                 {"If not",