Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extend the regroup syntax to support nested keys

This commit allows the user to use nested regroup keys. ie.

    {% regroup builds by host.name as hostname_list %}
  • Loading branch information...
commit e157b4dc695429bc9096e92dd53a2f3868970bf2 1 parent 165b1bf
Lukas Larsson authored May 18, 2012 garazdawi committed July 18, 2012
18  src/erlydtl_compiler.erl
@@ -585,8 +585,8 @@ body_ast(DjangoParseTree, Context, TreeWalker) ->
585 585
                 include_ast(unescape_string_literal(File), Args, Context#dtl_context.local_scopes, Context, TreeWalkerAcc);
586 586
             ({'include_only', {string_literal, _, File}, Args}, TreeWalkerAcc) ->
587 587
                 include_ast(unescape_string_literal(File), Args, [], Context, TreeWalkerAcc);
588  
-            ({'regroup', {ListVariable, {identifier, _, Attribute}, {identifier, _, NewVariable}}, Contents}, TreeWalkerAcc) ->
589  
-                regroup_ast(ListVariable, Attribute, NewVariable, Contents, Context, TreeWalkerAcc);
  588
+            ({'regroup', {ListVariable, Grouper, {identifier, _, NewVariable}}, Contents}, TreeWalkerAcc) ->
  589
+                regroup_ast(ListVariable, Grouper, NewVariable, Contents, Context, TreeWalkerAcc);
590 590
             ({'spaceless', Contents}, TreeWalkerAcc) ->
591 591
                 spaceless_ast(Contents, Context, TreeWalkerAcc);
592 592
             ({'ssi', Arg}, TreeWalkerAcc) ->
@@ -1038,20 +1038,26 @@ with_ast(ArgList, Contents, Context, TreeWalker) ->
1038 1038
                         erl_syntax:clause(lists:map(fun({_, Var}) -> Var end, NewScope), none,
1039 1039
                             [InnerAst])]), ArgAstList), merge_info(ArgInfo, InnerInfo)}, TreeWalker2}.
1040 1040
 
1041  
-regroup_ast(ListVariable, AttributeName, LocalVarName, Contents, Context, TreeWalker) ->
  1041
+regroup_ast(ListVariable, GrouperVariable, LocalVarName, Contents, Context, TreeWalker) ->
1042 1042
     {{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, Context, TreeWalker),
1043 1043
     NewScope = [{LocalVarName, erl_syntax:variable(lists:concat(["Var_", LocalVarName]))}],
1044 1044
 
1045 1045
     {{InnerAst, InnerInfo}, TreeWalker2} = body_ast(Contents, 
1046 1046
         Context#dtl_context{ local_scopes = [NewScope|Context#dtl_context.local_scopes] }, TreeWalker1),
1047 1047
 
1048  
-    {{erl_syntax:application(
  1048
+    Ast = {erl_syntax:application(
1049 1049
                 erl_syntax:fun_expr([
1050 1050
                         erl_syntax:clause([erl_syntax:variable(lists:concat(["Var_", LocalVarName]))], none,
1051 1051
                             [InnerAst])]), 
1052 1052
                 [erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(regroup),
1053  
-                        [ListAst, erl_syntax:atom(AttributeName)])]), merge_info(ListInfo, InnerInfo)},
1054  
-        TreeWalker2}.
  1053
+                        [ListAst, regroup_filter(GrouperVariable,[])])]), merge_info(ListInfo, InnerInfo)},
  1054
+    {Ast,TreeWalker2}.
  1055
+
  1056
+regroup_filter({attribute,{{identifier,_,Ident},Next}},Acc) ->
  1057
+    regroup_filter(Next,[erl_syntax:atom(Ident)|Acc]);
  1058
+regroup_filter({variable,{identifier,_,Var}},Acc) ->
  1059
+    erl_syntax:list([erl_syntax:atom(Var)|Acc]).
  1060
+
1055 1061
 
1056 1062
 for_loop_ast(IteratorList, LoopValue, Contents, {EmptyContentsAst, EmptyContentsInfo}, Context, TreeWalker) ->
1057 1063
     Vars = lists:map(fun({identifier, _, Iterator}) -> 
2  src/erlydtl_parser.yrl
@@ -336,7 +336,7 @@ IfNotEqualExpression -> Value : '$1'.
336 336
 EndIfNotEqualBraced -> open_tag endifnotequal_keyword close_tag.
337 337
 
338 338
 RegroupBlock -> RegroupBraced Elements EndRegroupBraced : {regroup, '$1', '$2'}.
339  
-RegroupBraced -> open_tag regroup_keyword Value by_keyword identifier as_keyword identifier close_tag : {'$3', '$5', '$7'}.
  339
+RegroupBraced -> open_tag regroup_keyword Value by_keyword Value as_keyword identifier close_tag : {'$3', '$5', '$7'}.
340 340
 EndRegroupBraced -> open_tag endregroup_keyword close_tag.
341 341
 
342 342
 SpacelessBlock -> open_tag spaceless_keyword close_tag Elements open_tag endspaceless_keyword close_tag : {spaceless, '$4'}.
11  src/erlydtl_runtime.erl
@@ -49,6 +49,13 @@ find_value(Key, Tuple) when is_tuple(Tuple) ->
49 49
             end
50 50
     end.
51 51
 
  52
+find_deep_value([Key|Rest],Item) ->
  53
+    case find_value(Key,Item) of
  54
+	undefined -> undefined;
  55
+	NewItem -> find_deep_value(Rest,NewItem)
  56
+    end;
  57
+find_deep_value([],Item) -> Item.
  58
+
52 59
 fetch_value(Key, Data) ->
53 60
     case find_value(Key, Data) of
54 61
         undefined ->
@@ -65,9 +72,9 @@ regroup([], _, []) ->
65 72
 regroup([], _, [[{grouper, LastGrouper}, {list, LastList}]|Acc]) ->
66 73
     lists:reverse([[{grouper, LastGrouper}, {list, lists:reverse(LastList)}]|Acc]);
67 74
 regroup([Item|Rest], Attribute, []) ->
68  
-    regroup(Rest, Attribute, [[{grouper, find_value(Attribute, Item)}, {list, [Item]}]]);
  75
+    regroup(Rest, Attribute, [[{grouper, find_deep_value(Attribute, Item)}, {list, [Item]}]]);
69 76
 regroup([Item|Rest], Attribute, [[{grouper, PrevGrouper}, {list, PrevList}]|Acc]) ->
70  
-    case find_value(Attribute, Item) of
  77
+    case find_deep_value(Attribute, Item) of
71 78
         Value when Value =:= PrevGrouper ->
72 79
             regroup(Rest, Attribute, [[{grouper, PrevGrouper}, {list, [Item|PrevList]}]|Acc]);
73 80
         Value ->

0 notes on commit e157b4d

Please sign in to comment.
Something went wrong with that request. Please try again.