Commits

Anonymous committed f37ff5f

added call custom tag (based on patch from Jeremey Latt) - not complete yet - test case missing

Comments (0)

Files changed (3)

src/erlydtl/erlydtl_compiler.erl

                 ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);                
             ({'ifnotequal', Args, Contents}, TreeWalkerAcc) ->
                 {IfAstInfo, TreeWalker1} = empty_ast(TreeWalkerAcc),
-                {ElseAstInfo, TreeWalker2} = body_ast(Contents, Context,TreeWalker1),
+                {ElseAstInfo, TreeWalker2} = body_ast(Contents, Context, TreeWalker1),
                 ifequalelse_ast(Args, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
             ({'ifnotequalelse', Args, IfContents, ElseContents}, TreeWalkerAcc) ->
                 {IfAstInfo, TreeWalker1} = body_ast(ElseContents, Context, TreeWalkerAcc),
             ({'load', Names}, TreeWalkerAcc) ->
                 load_ast(Names, Context, TreeWalkerAcc);
             ({'tag', {identifier, _, Name}, Args}, TreeWalkerAcc) ->
-                tag_ast(Name, Args, Context, TreeWalkerAcc)
+                tag_ast(Name, Args, Context, TreeWalkerAcc);            
+            ({'call', {'identifier', _, Name}}, TreeWalkerAcc) ->
+            	call_ast(Name, TreeWalkerAcc);
+            ({'call', {'identifier', _, Name}, {variable, With}}, TreeWalkerAcc) ->
+            	call_with_ast(Name, With, Context, TreeWalkerAcc)                
         end, TreeWalker, DjangoParseTree),   
     {AstList, {Info, TreeWalker3}} = lists:mapfoldl(
         fun({Ast, Info}, {InfoAcc, TreeWalkerAcc}) -> 
  tag_ast2(Source, TagParseTree, InterpretedArgs, Context, TreeWalker) ->
     with_dependency(Source, body_ast(TagParseTree, Context#dtl_context{
         local_scopes = [ InterpretedArgs | Context#dtl_context.local_scopes ],
-        parse_trail = [ Source | Context#dtl_context.parse_trail ]}, TreeWalker)).
+        parse_trail = [ Source | Context#dtl_context.parse_trail ]}, TreeWalker)).
+
+
+call_ast(Module, TreeWalkerAcc) ->
+    call_ast(Module, erl_syntax:variable("Variables"), #ast_info{}, TreeWalkerAcc).
+
+call_with_ast(Module, Variable, Context, TreeWalker) ->
+    {VarAst, VarName} = resolve_variable_ast(Variable, Context),
+    call_ast(Module, VarAst, #ast_info{var_names=[VarName]}, TreeWalker).
+        
+call_ast(Module, Variable, AstInfo, TreeWalker) ->
+     AppAst = erl_syntax:application(
+		erl_syntax:atom(Module),
+		erl_syntax:atom(render),
+		[Variable]),
+    RenderedAst = erl_syntax:variable("Rendered"),
+    OkAst = erl_syntax:clause(
+	      [erl_syntax:tuple([erl_syntax:atom(ok), RenderedAst])], 
+	      none,
+	      [RenderedAst]),
+    ReasonAst = erl_syntax:variable("Reason"),
+    ErrStrAst = erl_syntax:application(
+		  erl_syntax:atom(io_lib),
+		  erl_syntax:atom(format),
+		  [erl_syntax:string("error: ~p"), erl_syntax:list([ReasonAst])]),
+    ErrorAst = erl_syntax:clause(
+		 [erl_syntax:tuple([erl_syntax:atom(error), ReasonAst])], 
+		 none,
+		 [ErrStrAst]),
+    CallAst = erl_syntax:case_expr(AppAst, [OkAst, ErrorAst]),
+    {{CallAst, AstInfo}, TreeWalker}.

src/erlydtl/erlydtl_parser.yrl

     LoadNames
     
     CustomTag
-    Args.
+    Args
+    
+    CallTag
+    CallWithTag.
 
 Terminals
     autoescape_keyword
     block_keyword
+    call_keyword
     close_tag
     close_var
     comment_keyword
     open_var
     pipe
     string_literal
-    text.
+    text
+    with_keyword.
 
 Rootsymbol
     Elements.
 Elements -> Elements ExtendsTag : '$1' ++ ['$2'].
 Elements -> Elements IncludeTag : '$1' ++ ['$2'].
 Elements -> Elements LoadTag : '$1' ++ ['$2'].
-Elements -> Elements CustomTag : '$1' ++ ['$2'].
 Elements -> Elements BlockBlock : '$1' ++ ['$2'].
 Elements -> Elements ForBlock : '$1' ++ ['$2'].
 Elements -> Elements IfBlock : '$1' ++ ['$2'].
 Elements -> Elements IfNotEqualBlock : '$1' ++ ['$2'].
 Elements -> Elements AutoEscapeBlock : '$1' ++ ['$2'].
 Elements -> Elements CommentBlock : '$1' ++ ['$2'].
+Elements -> Elements CustomTag : '$1' ++ ['$2'].
+Elements -> Elements CallTag : '$1' ++ ['$2'].
+Elements -> Elements CallWithTag : '$1' ++ ['$2'].
 
 VariableBraced -> open_var Variable close_var : '$2'.
 
 LoadNames -> identifier : ['$1'].
 LoadNames -> LoadNames identifier : '$1' ++ ['$2'].
 
-CustomTag -> open_tag identifier Args close_tag : {tag, '$2', '$3'}.
-
-Args -> '$empty' : [].
-Args -> Args identifier equal Variable : '$1' ++ [{'$2', '$4'}].
-
 BlockBlock -> BlockBraced Elements EndBlockBraced : {block, '$1', '$2'}.
 BlockBraced -> open_tag block_keyword identifier close_tag : '$3'.
 EndBlockBraced -> open_tag endblock_keyword close_tag.
 
 Literal -> string_literal : '$1'.
 Literal -> number_literal : '$1'.
+
+CustomTag -> open_tag identifier Args close_tag : {tag, '$2', '$3'}.
+
+Args -> '$empty' : [].
+Args -> Args identifier equal Variable : '$1' ++ [{'$2', '$4'}].
+
+CallTag -> open_tag call_keyword identifier close_tag : {call, '$3'}.
+CallWithTag -> open_tag call_keyword identifier with_keyword Variable close_tag: {call, '$3', '$5'}.

src/erlydtl/erlydtl_scanner.erl

                             "not", "or", "and", "comment", "endcomment", "cycle", "firstof",
                             "ifchanged", "ifequal", "endifequal", "ifnotequal", "endifnotequal",
                             "now", "regroup", "spaceless", "endspaceless", "ssi", "templatetag",
-                            "load"], 
+                            "load", "call", "with"], 
                         Type = case lists:member(RevString, Keywords) of
                             true ->
                                 list_to_atom(RevString ++ "_keyword");