Browse Source

added load tag (not fully functional yet)

git-svn-id: http://erlydtl.googlecode.com/svn/trunk@94 a5195066-8e3e-0410-a82a-05b01b1b9875
rsaccon 17 years ago
parent
commit
451248e3e7

+ 1 - 0
demo/out/test_htmltags.html

@@ -1,3 +1,4 @@
+
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
   <head>

+ 1 - 0
demo/templates/test_htmltags.html

@@ -1,3 +1,4 @@
+{% load tag %}
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
   <head>

+ 0 - 0
priv/tags/flashvideo → priv/customtags/flashvideo


+ 31 - 22
src/erlydtl/erlydtl_compiler.erl

@@ -212,9 +212,7 @@ body_ast(DjangoParseTree, Context) ->
                 string_ast(Number);
             ({'variable', Variable}) ->
                 {Ast, VarName} = resolve_variable_ast(Variable, Context),
-                {format(Ast, Context), #ast_info{var_names = [VarName]}};
-            ({'tag', {identifier, _, Name}, Args}) ->
-                tag_ast(Name, Args, Context);
+                {format(Ast, Context), #ast_info{var_names = [VarName]}};              
             ({'include', {string_literal, _, File}}) ->
                 include_ast(unescape_string_literal(File), Context);
             ({'if', {variable, Variable}, Contents}) ->
@@ -240,7 +238,11 @@ body_ast(DjangoParseTree, Context) ->
             ({'apply_filter', Variable, Filter}) ->
                 filter_ast(Variable, Filter, Context);
             ({'for', {'in', IteratorList, Variable}, Contents}) ->
-                for_loop_ast(IteratorList, Variable, Contents, Context)
+                for_loop_ast(IteratorList, Variable, Contents, Context);
+            ({'load', Names}) ->
+                load_ast(Names, Context);
+            ({'tag', {identifier, _, Name}, Args}) ->
+                tag_ast(Name, Args, Context)
         end, DjangoParseTree),
     {AstList, Info} = lists:mapfoldl(
         fun({Ast, Info}, InfoAcc) -> 
@@ -476,23 +478,9 @@ for_loop_ast(IteratorList, {variable, Variable}, Contents, Context) ->
                         CounterVars0, ListAst])]),
                 Info#ast_info{var_names = [VarName]}}.
 
-%% TODO: implement "load" tag to make custom tags work like in original django
-tag_ast(Name, Args, Context) ->
-    InterpretedArgs = lists:map(fun
-            ({{identifier, _, Key}, {string_literal, _, Value}}) ->
-                {list_to_atom(Key), erl_syntax:string(unescape_string_literal(Value))};
-            ({{identifier, _, Key}, {variable, Value}}) ->
-                {list_to_atom(Key), format(resolve_variable_ast(Value, Context), Context)}
-        end, Args),
-    Source = filename:join([erlydtl_deps:get_base_dir(), "priv", "tags", Name]),
-    case parse(Source, Context#dtl_context.reader) of
-        {ok, TagParseTree} ->
-            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 ]}));
-        _ ->
-            {error, Name, "Loading tag source failed: " ++ Source}
-    end.
+load_ast(Names, Context) ->
+    io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, Names]),
+    {erl_syntax:list([]), #ast_info{}}.  
 
 unescape_string_literal(String) ->
     unescape_string_literal(string:strip(String, both, 34), [], noslash).
@@ -511,4 +499,25 @@ unescape_string_literal("t" ++ Rest, Acc, slash) ->
     unescape_string_literal(Rest, ["\t" | Acc], noslash);
 unescape_string_literal([C | Rest], Acc, slash) ->
     unescape_string_literal(Rest, [C | Acc], noslash).
-    
+
+
+%%-------------------------------------------------------------------
+%% Custom tags
+%%-------------------------------------------------------------------
+
+tag_ast(Name, Args, Context) ->
+    InterpretedArgs = lists:map(fun
+            ({{identifier, _, Key}, {string_literal, _, Value}}) ->
+                {list_to_atom(Key), erl_syntax:string(unescape_string_literal(Value))};
+            ({{identifier, _, Key}, {variable, Value}}) ->
+                {list_to_atom(Key), format(resolve_variable_ast(Value, Context), Context)}
+        end, Args),
+    Source = filename:join([erlydtl_deps:get_base_dir(), "priv", "customtags", Name]),
+    case parse(Source, Context#dtl_context.reader) of
+        {ok, TagParseTree} ->
+            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 ]}));
+        _ ->
+            {error, Name, "Loading tag source failed: " ++ Source}
+    end.

+ 13 - 4
src/erlydtl/erlydtl_parser.yrl

@@ -41,9 +41,6 @@ Nonterminals
     ExtendsTag
     IncludeTag
 
-    CustomTag
-    Args
-
     BlockBlock
     BlockBraced
     EndBlockBraced
@@ -79,7 +76,13 @@ Nonterminals
     EndAutoEscapeBraced
 
     Variable
-    Filter.
+    Filter
+    
+    LoadTag
+    LoadNames
+    
+    CustomTag
+    Args.
 
 Terminals
     autoescape_keyword
@@ -107,6 +110,7 @@ Terminals
     ifnotequal_keyword
     in_keyword
     include_keyword
+    load_keyword
     not_keyword
     number_literal
     open_tag
@@ -123,6 +127,7 @@ Elements -> Elements text : '$1' ++ ['$2'].
 Elements -> Elements VariableBraced : '$1' ++ ['$2'].
 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'].
@@ -143,6 +148,10 @@ Variable -> number_literal : '$1'.
 ExtendsTag -> open_tag extends_keyword string_literal close_tag : {extends, '$3'}.
 IncludeTag -> open_tag include_keyword string_literal close_tag : {include, '$3'}.
 
+LoadTag -> open_tag load_keyword LoadNames close_tag : {load, '$3'}.
+LoadNames -> identifier : ['$1'].
+LoadNames -> LoadNames identifier : '$1' ++ ['$2'].
+
 CustomTag -> open_tag identifier Args close_tag : {tag, '$2', '$3'}.
 
 Args -> '$empty' : [].

+ 2 - 1
src/erlydtl/erlydtl_scanner.erl

@@ -61,7 +61,8 @@ scan([], Scanned, _, in_text) ->
                             "extends", "autoescape", "endautoescape", "if", "else", "endif",
                             "not", "or", "and", "comment", "endcomment", "cycle", "firstof",
                             "ifchanged", "ifequal", "endifequal", "ifnotequal", "endifnotequal",
-                            "now", "regroup", "spaceless", "endspaceless", "ssi", "templatetag"], 
+                            "now", "regroup", "spaceless", "endspaceless", "ssi", "templatetag",
+                            "load"], 
                         Type = case lists:member(RevString, Keywords) of
                             true ->
                                 list_to_atom(RevString ++ "_keyword");