Browse Source

completed the load tag implementation

git-svn-id: http://erlydtl.googlecode.com/svn/trunk@99 a5195066-8e3e-0410-a82a-05b01b1b9875
rsaccon 17 years ago
parent
commit
8dabac4fbf
2 changed files with 23 additions and 16 deletions
  1. 1 1
      demo/templates/test_customtags.html
  2. 22 15
      src/erlydtl/erlydtl_compiler.erl

+ 1 - 1
demo/templates/test_customtags.html

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

+ 22 - 15
src/erlydtl/erlydtl_compiler.erl

@@ -145,6 +145,7 @@ compile(File, Module, DocRoot, Vars, Reader, OutDir) ->
                     Other
             end;
         Error ->
+            io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, Error]),
             Error
     end.
 
@@ -489,8 +490,9 @@ for_loop_ast(IteratorList, {variable, Variable}, Contents, Context, TreeWalker)
                         CounterVars0, ListAst])]),
                 Info#ast_info{var_names = [VarName]}}, TreeWalker2}.
 
-load_ast(_Names, _Context, TreeWalker) ->
-    {{erl_syntax:list([]), #ast_info{}}, TreeWalker}.  
+load_ast(Names, _Context, TreeWalker) ->
+    CustomTags = lists:merge([X || {identifier, _ , X} <- Names], TreeWalker#treewalker.custom_tags),
+    {{erl_syntax:list([]), #ast_info{}}, TreeWalker#treewalker{custom_tags = CustomTags}}.  
 
 unescape_string_literal(String) ->
     unescape_string_literal(string:strip(String, both, 34), [], noslash).
@@ -516,18 +518,23 @@ unescape_string_literal([C | Rest], Acc, slash) ->
 %%-------------------------------------------------------------------
 
 tag_ast(Name, Args, Context, TreeWalker) ->
-    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 ]}, TreeWalker));
+    case lists:member(Name, TreeWalker#treewalker.custom_tags) of
+        true ->
+            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 ]}, TreeWalker));
+                _ ->
+                    {error, Name, "Loading tag source failed: " ++ Source}
+            end;
         _ ->
-            {error, Name, "Loading tag source failed: " ++ Source}
+            {error, Name, "Custom tag not loaded"}
     end.