Permalink
Browse files

added ifequal tag

  • Loading branch information...
rsaccon committed Jan 21, 2008
1 parent ba6f11e commit 15fa5ce1e709f4599872e7b54a990ee1be760abd
View
@@ -0,0 +1,28 @@
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+
+
+else: var1="foo" and var3="bar" are not equal
+
+
+
+if: "foo" and "foo" are equal
+
+
+
+else: "foo" and "bar" are not equal
+
+
+
+if: 99 and 99 are equal
+
+
+
+else: 77 and 99 are not equal
@@ -0,0 +1,28 @@
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+
+
+else: var1="foo" and var3="bar" are not equal
+
+
+
+if: "foo" and "foo" are equal
+
+
+
+else: "foo" and "bar" are not equal
+
+
+
+if: 99 and 99 are equal
+
+
+
+else: 77 and 99 are not equal
@@ -0,0 +1,43 @@
+{% ifequal var1 var2 %}
+if: var1="foo" and var2="foo" are equal
+{% endifequal %}
+
+{% ifequal var1 var2 %}
+if: var1="foo" and var2="foo" are equal
+{% else %}
+else: var1="foo" and var2="foo" are not equal
+{% endifequal %}
+
+{% ifequal var1 var3 %}
+if: var1="foo" and var3="bar" are equal
+{% endifequal %}
+
+{% ifequal var1 var3 %}
+if: var1="foo" and var3="bar" are equal
+{% else %}
+else: var1="foo" and var3="bar" are not equal
+{% endifequal %}
+
+{% ifequal "foo" "foo" %}
+if: "foo" and "foo" are equal
+{% else %}
+else: "foo" and "foo" are not equal
+{% endifequal %}
+
+{% ifequal "foo" "bar" %}
+if: "foo" and "bar" are equal
+{% else %}
+else: "foo" and "bar" are not equal
+{% endifequal %}
+
+{% ifequal 99 99 %}
+if: 99 and 99 are equal
+{% else %}
+else: 99 and 99" are not equal
+{% endifequal %}
+
+{% ifequal 77 99 %}
+if: 77 and 99 are equal
+{% else %}
+else: 77 and 99 are not equal
+{% endifequal %}
@@ -0,0 +1,43 @@
+{% ifequal var1 var2 %}
+if: var1="foo" and var2="foo" are equal
+{% endifequal %}
+
+{% ifequal var1 var2 %}
+if: var1="foo" and var2="foo" are equal
+{% else %}
+else: var1="foo" and var2="foo" are not equal
+{% endifequal %}
+
+{% ifequal var1 var3 %}
+if: var1="foo" and var3="bar" are equal
+{% endifequal %}
+
+{% ifequal var1 var3 %}
+if: var1="foo" and var3="bar" are equal
+{% else %}
+else: var1="foo" and var3="bar" are not equal
+{% endifequal %}
+
+{% ifequal "foo" "foo" %}
+if: "foo" and "foo" are equal
+{% else %}
+else: "foo" and "foo" are not equal
+{% endifequal %}
+
+{% ifequal "foo" "bar" %}
+if: "foo" and "bar" are equal
+{% else %}
+else: "foo" and "bar" are not equal
+{% endifequal %}
+
+{% ifequal 99 99 %}
+if: 99 and 99 are equal
+{% else %}
+else: 99 and 99" are not equal
+{% endifequal %}
+
+{% ifequal 77 99 %}
+if: 77 and 99 are equal
+{% else %}
+else: 77 and 99 are not equal
+{% endifequal %}
View
@@ -79,7 +79,9 @@ compile_all() ->
compile("for_records_preset"),
compile("htmltags"),
compile("if"),
- compile("if_preset"),
+ compile("if_preset"),
+ compile("ifequal"),
+ compile("ifequal_preset"),
compile("include"),
compile("var"),
compile("var_preset").
@@ -108,12 +110,19 @@ compile("autoescape" = Name) ->
compile(Name, ".html", []);
compile("if" = Name) ->
- compile(Name, ".html", []);
+ compile(Name, ".html", []);
compile("if_preset" = Name) ->
Vars = [{var1, "something"}],
compile(Name, ".html", Vars);
+compile("ifequal" = Name) ->
+ compile(Name, ".html", []);
+
+compile("ifequal_preset" = Name) ->
+ Vars = [{var1, "foo"}, {var2, "foo"}],
+ compile(Name, ".html", Vars);
+
compile("filters" = Name) ->
compile(Name, ".html", []);
@@ -188,7 +197,9 @@ render_all() ->
render("for_records_preset"),
render("htmltags"),
render("if"),
- render("if_preset"),
+ render("if_preset"),
+ render("ifequal"),
+ render("ifequal_preset"),
render("include"),
render("var"),
render("var_preset").
@@ -215,11 +226,17 @@ render("include" = Name) ->
render(Name, [{var1, "foostring1"}, {var2, "foostring2"}]);
render("if" = Name) ->
- render(Name, [{var1, "something"}]);
+ render(Name, [{var1, "something"}]);
render("if_preset" = Name) ->
render(Name, []);
+
+render("ifequal" = Name) ->
+ render(Name, [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}]);
+render("ifequal_preset" = Name) ->
+ render(Name, [{var3, "bar"}]);
+
render("for" = Name) ->
render(Name, [{fruit_list, ["apple", "banana", "coconut"]}]);
@@ -283,3 +300,4 @@ render(Name, Args) ->
%%====================================================================
%% Internal functions
%%====================================================================
+
@@ -47,7 +47,7 @@
-record(ast_info, {
dependencies = [],
- template_vars = [],
+ var_names = [],
pre_render_asts = []
}).
@@ -209,8 +209,8 @@ body_ast(DjangoParseTree, Context) ->
({'number_literal', _Pos, Number}) ->
string_ast(Number);
({'variable', Variable}) ->
- {Ast, Var} = resolve_variable_ast(Variable, Context),
- {Ast, #ast_info{template_vars = [Var]}};
+ {Ast, VarName} = resolve_variable_ast(Variable, Context),
+ {Ast, #ast_info{var_names = [VarName]}};
({'tag', {identifier, _, Name}, Args}) ->
tag_ast(Name, Args, Context);
({'include', {string_literal, _, File}}) ->
@@ -225,6 +225,13 @@ body_ast(DjangoParseTree, Context) ->
({'ifelse', {'not', {variable, Variable}}, IfContents, ElseContents}) ->
ifelse_ast(Variable, body_ast(ElseContents, Context),
body_ast(IfContents, Context), Context);
+
+ ({'ifequal', Args, Contents}) ->
+ ifequalelse_ast(Args, body_ast(Contents, Context), empty_ast(), Context);
+ ({'ifequalelse', Args, IfContents, ElseContents}) ->
+ ifequalelse_ast(Args,body_ast(IfContents, Context),
+ body_ast(ElseContents, Context), Context);
+
({'apply_filter', Variable, Filter}) ->
filter_ast(Variable, Filter, Context);
({'for', {'in', {identifier, _, Iterator}, {identifier, _, List}}, Contents}) ->
@@ -242,7 +249,7 @@ body_ast(DjangoParseTree, Context) ->
Val ->
[erl_syntax:abstract(Val) | Acc]
end
- end, [], Info#ast_info.template_vars),
+ end, [], Info#ast_info.var_names),
case PresetVars of
[] ->
{Ast, merge_info(Info, InfoAcc)};
@@ -266,10 +273,10 @@ merge_info(Info1, Info2) ->
lists:merge(
lists:sort(Info1#ast_info.dependencies),
lists:sort(Info2#ast_info.dependencies)),
- template_vars =
+ var_names =
lists:merge(
- lists:sort(Info1#ast_info.template_vars),
- lists:sort(Info2#ast_info.template_vars)),
+ lists:sort(Info1#ast_info.var_names),
+ lists:sort(Info2#ast_info.var_names)),
pre_render_asts =
lists:merge(
Info1#ast_info.pre_render_asts,
@@ -382,9 +389,9 @@ auto_escape(Value, Context) ->
end.
ifelse_ast(Variable, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context) ->
- Info2 = merge_info(IfContentsInfo, ElseContentsInfo),
- TemplateVars = Info2#ast_info.template_vars,
- {Ast, Var} = resolve_ifvariable_ast(Variable, Context),
+ Info = merge_info(IfContentsInfo, ElseContentsInfo),
+ VarNames = Info#ast_info.var_names,
+ {Ast, VarName} = resolve_ifvariable_ast(Variable, Context),
{erl_syntax:case_expr(Ast,
[erl_syntax:clause([erl_syntax:string("")], none,
[ElseContentsAst]),
@@ -394,7 +401,29 @@ ifelse_ast(Variable, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseCont
[ElseContentsAst]),
erl_syntax:clause([erl_syntax:underscore()], none,
[IfContentsAst])
- ]), Info2#ast_info{template_vars = [Var | TemplateVars]}}.
+ ]), Info#ast_info{var_names = [VarName | VarNames]}}.
+
+ifequalelse_ast(Args, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context) ->
+ Info = merge_info(IfContentsInfo, ElseContentsInfo),
+ {[Arg1Ast, Arg2Ast], VarNames} = lists:foldl(fun
+ (X, {Asts, AccVarNames}) ->
+ case X of
+ {variable, Var} ->
+ {Ast, VarName} = resolve_ifvariable_ast(Var, Context),
+ {[Ast | Asts], [VarName | AccVarNames]};
+ {_, _, Literal} ->
+ {[erl_syntax:string(Literal) | Asts], AccVarNames}
+ end
+ end,
+ {[], Info#ast_info.var_names},
+ Args),
+ Ast = erl_syntax:application(none, erl_syntax:atom(apply), [erl_syntax:fun_expr(
+ [erl_syntax:clause([erl_syntax:variable("Arg1"), erl_syntax:variable("Arg2")], none,
+ [erl_syntax:case_expr(erl_syntax:variable("Arg1"),
+ [erl_syntax:clause([erl_syntax:variable("Arg2")], none, [IfContentsAst]),
+ erl_syntax:clause([erl_syntax:underscore()], none, [ElseContentsAst])])])]),
+ erl_syntax:list([Arg1Ast, Arg2Ast])]),
+ {Ast, Info#ast_info{var_names = VarNames}}.
for_loop_ast(Iterator, List, Contents, Context) ->
{InnerAst, Info} = body_ast(Contents,
@@ -405,7 +434,7 @@ for_loop_ast(Iterator, List, Contents, Context) ->
[erl_syntax:fun_expr([
erl_syntax:clause([erl_syntax:variable("Var_" ++ Iterator)],
none, [InnerAst])]),
- resolve_variable_name_ast(list_to_atom(List), Context)]), Info#ast_info{template_vars = [List]}}.
+ resolve_variable_name_ast(list_to_atom(List), Context)]), Info#ast_info{var_names = [List]}}.
for_list_loop_ast(IteratorList, List, Contents, Context) ->
Vars = erl_syntax:list(lists:map(
@@ -420,7 +449,7 @@ for_list_loop_ast(IteratorList, List, Contents, Context) ->
{erl_syntax:application(erl_syntax:atom(lists), erl_syntax:atom(map),
[erl_syntax:fun_expr([erl_syntax:clause(
[Vars], none, [InnerAst])]),
- resolve_variable_name_ast(list_to_atom(List), Context)]), Info#ast_info{template_vars = [List]}}.
+ resolve_variable_name_ast(list_to_atom(List), Context)]), Info#ast_info{var_names = [List]}}.
%% TODO: implement "laod" tag to make custom tags work like in original django
tag_ast(Name, Args, Context) ->
Oops, something went wrong.

0 comments on commit 15fa5ce

Please sign in to comment.