Commits

Anonymous committed c3d593c

added custom directory for custom tags as template compile option

Comments (0)

Files changed (10)

demo/out/test_custom_tags.html

+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title>Test variable</title>								 
+  </head>
+  <body>
+	before
+	<object id="myvideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="800" height="600">
+	<param name="movie" value="/static/mediaplayer.swf">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
+    <!--[if !IE]>-->
+    <object type="application/x-shockwave-flash" data="/static/mediaplayer.swf" width="800" height="620">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
+    <!--<![endif]-->
+    <h2>To view the Video:</h2>
+    <p>
+    	<a href="http://www.adobe.com/go/getflashplayer">
+        	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="Get Adobe Flash player">
+    	</a>
+    </p>
+    <!--[if !IE]>-->
+    </object>
+    <!--<![endif]-->
+</object>
+	after
+  </body>
+</html>

demo/out/test_customtags.html

-
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-<html>
-  <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-    <title>Test variable</title>								 
-  </head>
-  <body>
-	before
-	<object id="myvideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="800" height="600">
-	<param name="movie" value="/static/mediaplayer.swf">
-    <param name="allowfullscreen" value="true">
-    <param name="menu" value="false">
-    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
-    <!--[if !IE]>-->
-    <object type="application/x-shockwave-flash" data="/static/mediaplayer.swf" width="800" height="620">
-    <param name="allowfullscreen" value="true">
-    <param name="menu" value="false">
-    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
-    <!--<![endif]-->
-    <h2>To view the Video:</h2>
-    <p>
-    	<a href="http://www.adobe.com/go/getflashplayer">
-        	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="Get Adobe Flash player">
-    	</a>
-    </p>
-    <!--[if !IE]>-->
-    </object>
-    <!--<![endif]-->
-</object>
-	after
-  </body>
-</html>

demo/templates/test_custom_tags.html

+{% load flashvideo %}
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title>Test variable</title>								 
+  </head>
+  <body>
+	before
+	{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" %}
+	after
+  </body>
+</html>

demo/templates/test_custom_tags_error.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title>Test variable</title>								 
+  </head>
+  <body>
+	before
+	{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" %}
+	after
+  </body>
+</html>

demo/templates/test_customtags.html

-{% load flashvideo %}
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-<html>
-  <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-    <title>Test variable</title>								 
-  </head>
-  <body>
-	before
-	{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" %}
-	after
-  </body>
-</html>

demo/templates/test_customtags_error.html

-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-<html>
-  <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-    <title>Test variable</title>								 
-  </head>
-  <body>
-	before
-	{% flashvideo dom_id="myvideo" width="800" height="600" static="/static" path_to_video="/myvid.mp4" path_to_preview_image="/mypic.jpg" %}
-	after
-  </body>
-</html>

priv/custom_tags/flashvideo

+<object id="{{ dom_id }}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{{ width }}" height="{{ height }}">
+	<param name="movie" value="{{ static }}/mediaplayer.swf">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file={{ path_to_video }}&image={{ path_to_preview_image }}">
+    <!--[if !IE]>-->
+    <object type="application/x-shockwave-flash" data="{{ static }}/mediaplayer.swf" width="800" height="620">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file={{ path_to_video }}&image={{ path_to_preview_image }}">
+    <!--<![endif]-->
+    <h2>To view the Video:</h2>
+    <p>
+    	<a href="http://www.adobe.com/go/getflashplayer">
+        	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="Get Adobe Flash player">
+    	</a>
+    </p>
+    <!--[if !IE]>-->
+    </object>
+    <!--<![endif]-->
+</object>

priv/customtags/flashvideo

-<object id="{{ dom_id }}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{{ width }}" height="{{ height }}">
-	<param name="movie" value="{{ static }}/mediaplayer.swf">
-    <param name="allowfullscreen" value="true">
-    <param name="menu" value="false">
-    <param name="flashvars" value="file={{ path_to_video }}&image={{ path_to_preview_image }}">
-    <!--[if !IE]>-->
-    <object type="application/x-shockwave-flash" data="{{ static }}/mediaplayer.swf" width="800" height="620">
-    <param name="allowfullscreen" value="true">
-    <param name="menu" value="false">
-    <param name="flashvars" value="file={{ path_to_video }}&image={{ path_to_preview_image }}">
-    <!--<![endif]-->
-    <h2>To view the Video:</h2>
-    <p>
-    	<a href="http://www.adobe.com/go/getflashplayer">
-        	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"alt="Get Adobe Flash player">
-    	</a>
-    </p>
-    <!--[if !IE]>-->
-    </object>
-    <!--<![endif]-->
-</object>

src/demo/erlydtl_demo.erl

 compile_all() ->
     compile("autoescape"),
     compile("comment"),
-    compile("customtags"),
-    compile("customtags_error"),
+    compile("custom_tags"),
+    compile("custom_tags_error"),
     compile("extends"),
     compile("filters"),
     compile("for"),
     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) ->
 render_all() ->
     render("autoescape"),
     render("comment"),
-    render("customtags"),
+    render("custom_tags"),
     render("extends"),
     render("filters"),
     render("for"),
 render("var_error" = Name) ->
     render(Name, [{var1, "foostring1"}]);
         
-render("customtags" = Name) ->
+render("custom_tags" = Name) ->
     render(Name, []);
                 
 render(Name) ->

src/erlydtl/erlydtl_compiler.erl

 -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 = [], 
     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
                     ({{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)).