Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support {% elif %} clause in if template tag syntax #35

Merged
merged 2 commits into from

2 participants

Björn-Egil Dahlberg Evan Miller
Björn-Egil Dahlberg

I have implemented support for {% elif Expr %} clause like the if template tag in Django 1.4.

Example:

<td>
    {% if b.status == "done" %}
    <span class="badge badge-success">{{b.status}}</span>
    {% elif b.status == "aborted" %}
    <span class="badge badge-important">{{b.status}}</span>
    {% elif b.status == "timeout" %}
    <span class="badge badge-warning">{{b.status}}</span>
    {% else %}
    <span class="badge badge-info">{{b.status}}</span>
    {% endif %}
</td>

Unit tests for elif clauses are also included in the branch.

If this is something you want, merge it as-is or give a shout out to me if something needs polishing.

Regards,
Björn-Egil

Evan Miller
Owner

Love it, thanks!

Evan Miller evanmiller merged commit a61e741 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2012
  1. Björn-Egil Dahlberg

    Add unit tests for {% elif Expr %} syntax

    psyeugenic authored psyeugenic committed
  2. Björn-Egil Dahlberg

    Add support for {% elif Expr %} syntax

    psyeugenic authored psyeugenic committed
This page is out of date. Refresh to see the latest.
4 src/erlydtl_compiler.erl
View
@@ -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),
12 src/erlydtl_parser.yrl
View
@@ -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
2  src/erlydtl_scanner.erl
View
@@ -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 tests/src/erlydtl_unittests.erl
View
@@ -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",
Something went wrong with that request. Please try again.