Просмотр исходного кода

added custom directory for custom tags as template compile option

git-svn-id: http://erlydtl.googlecode.com/svn/trunk@102 a5195066-8e3e-0410-a82a-05b01b1b9875
rsaccon 17 лет назад
Родитель
Сommit
ad9002b227

+ 0 - 0
demo/out/test_customtags.html → demo/out/test_custom_tags.html


+ 0 - 0
demo/templates/test_customtags.html → demo/templates/test_custom_tags.html


+ 0 - 0
demo/templates/test_customtags_error.html → demo/templates/test_custom_tags_error.html


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


+ 6 - 6
src/demo/erlydtl_demo.erl

@@ -69,8 +69,8 @@ create_parser() ->
 compile_all() ->
     compile("autoescape"),
     compile("comment"),
-    compile("customtags"),
-    compile("customtags_error"),
+    compile("custom_tags"),
+    compile("custom_tags_error"),
     compile("extends"),
     compile("filters"),
     compile("for"),
@@ -171,10 +171,10 @@ compile("for_records_preset" = Name) ->
     Var = [{software_links, [Link1, Link2, Link3]}],
     compile(Name, ".html", Var);
     
-compile("customtags" = Name) ->
+compile("custom_tags" = Name) ->
     compile(Name, ".html", []);
 
-compile("customtags_error" = Name) ->
+compile("custom_tags_error" = Name) ->
     compile(Name, ".html", []);
                   
 compile(Name) ->
@@ -212,7 +212,7 @@ compile(Name, Ext, Vars) ->
 render_all() ->
     render("autoescape"),
     render("comment"),
-    render("customtags"),
+    render("custom_tags"),
     render("extends"),
     render("filters"),
     render("for"),
@@ -310,7 +310,7 @@ render("var_preset" = Name) ->
 render("var_error" = Name) ->
     render(Name, [{var1, "foostring1"}]);
         
-render("customtags" = Name) ->
+render("custom_tags" = Name) ->
     render(Name, []);
                 
 render(Name) ->

+ 41 - 14
src/erlydtl/erlydtl_compiler.erl

@@ -35,7 +35,7 @@
 -author('rsaccon@gmail.com').
 -author('emmiller@gmail.com').
 
--export([compile/2, compile/3, compile/4, compile/5, compile/6, parse/2, scan/2, body_ast/3]).
+-export([compile/2, compile/3, compile/4, compile/5, compile/6,compile/7, parse/2, scan/2, body_ast/3]).
 
 -record(dtl_context, {
     local_scopes = [], 
@@ -64,16 +64,20 @@ compile(File, Module, DocRoot) ->
     compile(File, Module, DocRoot, []).
 
 compile(File, Module, DocRoot, Vars) ->
-    compile(File, Module, DocRoot, Vars, {file, read_file}).
+    compile(File, Module, DocRoot, Vars, []).
         
-compile(File, Module, DocRoot, Vars, Reader) ->
-    compile(File, Module, DocRoot, Vars, Reader, "ebin").
+compile(File, Module, DocRoot, Vars, CustomTagsDir) ->
+    compile(File, Module, DocRoot, Vars, CustomTagsDir, {file, read_file}).
+        
+compile(File, Module, DocRoot, Vars, CustomTagsDir, Reader) ->
+    compile(File, Module, DocRoot, Vars, CustomTagsDir, Reader, "ebin").
 
-compile(File, Module, DocRoot, Vars, Reader, OutDir) ->   
+compile(File, Module, DocRoot, Vars, CustomTagsDir, Reader, OutDir) ->   
     case parse(File, Reader) of
         {ok, DjangoParseTree} ->        
             try body_ast(DjangoParseTree, #dtl_context{
                     doc_root = DocRoot,
+                    custom_tags_dir = CustomTagsDir,
                     parse_trail = [File], preset_vars = Vars, reader = Reader}, #treewalker{}) of
                 {{Ast, Info}, _} ->
                     case compile:forms(forms(File, Module, Ast, Info), []) of
@@ -526,15 +530,38 @@ tag_ast(Name, Args, Context, TreeWalker) ->
                     ({{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));
+            DefaultFilePath = filename:join([erlydtl_deps:get_base_dir(), "priv", "custom_tags", Name]),
+            case Context#dtl_context.custom_tags_dir of
+                [] ->
+                    case parse(DefaultFilePath, Context#dtl_context.reader) of
+                        {ok, TagParseTree} ->
+                            tag_ast2(DefaultFilePath, TagParseTree, InterpretedArgs, Context, TreeWalker);
+                        _ ->
+                            Reason = lists:concat(["Loading tag source for '", Name, "' failed: ", 
+                                DefaultFilePath]),
+                            throw({error, Reason})
+                    end;
                 _ ->
-                    throw({error, Name, "Loading tag source failed: " ++ Source})
+                    CustomFilePath = filename:join([Context#dtl_context.custom_tags_dir, Name]),
+                    case parse(CustomFilePath, Context#dtl_context.reader) of
+                        {ok, TagParseTree} ->
+                            tag_ast2(CustomFilePath, TagParseTree, InterpretedArgs, Context, TreeWalker);
+                        _ ->
+                            case parse(DefaultFilePath, Context#dtl_context.reader) of
+                                {ok, TagParseTree} ->
+                                    tag_ast2(DefaultFilePath, TagParseTree, InterpretedArgs, Context, TreeWalker);
+                                _ ->
+                                    Reason = lists:concat(["Loading tag source for '", Name, "' failed: ", 
+                                        CustomFilePath, ", ", DefaultFilePath]),
+                                    throw({error, Reason})
+                            end
+                    end
             end;
         _ ->
-            throw({error, lists:concat(["Custom tag not loaded: ", Name])})
-    end.
+            throw({error, lists:concat(["Custom tag '", Name, "' not loaded"])})
+    end.
+ 
+ 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)).