erlydtl_demo.erl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. %%%-------------------------------------------------------------------
  2. %%% File: erlydtl_demo.erl
  3. %%% @author Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
  4. %%% @copyright 2007 Roberto Saccon
  5. %%% @doc
  6. %%%
  7. %%% @end
  8. %%%
  9. %%% The MIT License
  10. %%%
  11. %%% Copyright (c) 2007 Roberto Saccon
  12. %%%
  13. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  14. %%% of this software and associated documentation files (the "Software"), to deal
  15. %%% in the Software without restriction, including without limitation the rights
  16. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. %%% copies of the Software, and to permit persons to whom the Software is
  18. %%% furnished to do so, subject to the following conditions:
  19. %%%
  20. %%% The above copyright notice and this permission notice shall be included in
  21. %%% all copies or substantial portions of the Software.
  22. %%%
  23. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. %%% THE SOFTWARE.
  30. %%%
  31. %%% @since 2007-11-17 by Roberto Saccon
  32. %%%-------------------------------------------------------------------
  33. -module(erlydtl_demo).
  34. -author('rsaccon@gmail.com').
  35. %% API
  36. -export([compile_all/0, compile/1, compile/2, render_all/0, render/1, preset/1]).
  37. %%====================================================================
  38. %% API
  39. %%====================================================================
  40. %%--------------------------------------------------------------------
  41. %% @spec () -> any()
  42. %% @doc compiles the templates to beam files
  43. %% @end
  44. %%--------------------------------------------------------------------
  45. compile_all() ->
  46. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  47. io:format("Compiling folder: ~p~n", [DocRoot]),
  48. filelib:fold_files(DocRoot,
  49. "\.html$|\.css$",
  50. true,
  51. fun(Path, _Acc) ->
  52. Module = filename:rootname(filename:basename(Path)),
  53. % case erlydtl_server:compile(Path, DocRoot, Module, {?MODULE, preset}) of
  54. case erlydtl_compiler:compile(Path, DocRoot, Module) of
  55. ok ->
  56. io:format("compile success: ~p~n",[Module]);
  57. _ ->
  58. io:format("compile failure: ~p~n",[Module])
  59. end
  60. end,
  61. []).
  62. %%--------------------------------------------------------------------
  63. %% @spec (string()) -> any()
  64. %% @doc
  65. %% compiles the template to beam files
  66. %% @end
  67. %%--------------------------------------------------------------------
  68. compile("var" = Name) ->
  69. compile(Name, ".html");
  70. compile("extends" = Name) ->
  71. compile(Name, ".html");
  72. compile("include" = Name) ->
  73. compile(Name, ".html");
  74. compile("autoescape" = Name) ->
  75. compile(Name, ".html");
  76. compile("if" = Name) ->
  77. compile(Name, ".html");
  78. compile("filters" = Name) ->
  79. compile(Name, ".html");
  80. compile("comment" = Name) ->
  81. compile(Name, ".html");
  82. compile("for" = Name) ->
  83. compile(Name, ".html");
  84. compile("for_records" = Name) ->
  85. compile(Name, ".html");
  86. compile("htmltags" = Name) ->
  87. compile(Name, ".html");
  88. compile("csstags" = Name) ->
  89. compile(Name, ".css");
  90. compile("var_preset" = Name) ->
  91. compile(Name, ".html");
  92. compile("for_preset" = Name) ->
  93. compile(Name, ".html");
  94. compile("for_records_preset" = Name) ->
  95. compile(Name, ".html");
  96. compile(Name) ->
  97. io:format("No such template: ~p~n",[Name]).
  98. %%--------------------------------------------------------------------
  99. %% @spec (string(), string()) -> any()
  100. %% @doc
  101. %% compiles the template to beam files
  102. %% @end
  103. %%--------------------------------------------------------------------
  104. compile(Name, Ext) ->
  105. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  106. Module = "test_" ++ Name,
  107. Path = filename:join([DocRoot, Module ++ Ext]),
  108. % case erlydtl_server:compile(Path, DocRoot, Module, {?MODULE, preset}) of
  109. case erlydtl_compiler:compile(Path, DocRoot, Module) of
  110. ok ->
  111. io:format("compile success: ~p~n",[Module]);
  112. _ ->
  113. io:format("compile failure: ~p~n",[Module])
  114. end.
  115. %%--------------------------------------------------------------------
  116. %% @spec () -> any()
  117. %% @doc renders template to a file
  118. %% @end
  119. %%--------------------------------------------------------------------
  120. render_all() ->
  121. render("autoescape"),
  122. render("comment"),
  123. render("csstags"),
  124. render("extends"),
  125. render("filters"),
  126. render("for"),
  127. render("for_list"),
  128. render("for_preset"),
  129. render("for_records"),
  130. render("for_records_preset"),
  131. render("htmltags"),
  132. render("if"),
  133. render("include"),
  134. render("var"),
  135. render("var_preset").
  136. %%--------------------------------------------------------------------
  137. %% @spec (string()) -> ok()
  138. %% @doc renders template to a file
  139. %% @end
  140. %%--------------------------------------------------------------------
  141. render("var" = Name) ->
  142. render(Name, [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}]);
  143. render("filters" = Name) ->
  144. render(Name, [{'list', ["eins", "zwei", "drei"]}]);
  145. render("include" = Name) ->
  146. render(Name, [{var1, "foostring1"}, {var2, "foostring2"}]);
  147. render("autoescape" = Name) ->
  148. render(Name, [{var1, "<b>bold</b>"}]);
  149. render("if" = Name) ->
  150. render(Name, [{var1, "something"}]);
  151. render("extends" = Name) ->
  152. render(Name, [{base_var, "base-barstring"}, {test_var, "test-barstring"}]);
  153. render("comment" = Name) ->
  154. render(Name, []);
  155. render("for" = Name) ->
  156. render(Name, [{fruit_list, ["apple", "banana", "coconut"]}]);
  157. render("for_list" = Name) ->
  158. render(Name, [{fruit_list, [["apple", "apples"], ["banana", "bananas"], ["coconut", "coconuts"]]}]);
  159. render("for_records" = Name) ->
  160. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  161. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  162. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  163. render(Name, [{link_list, [Link1, Link2, Link3]}]);
  164. render("htmltags" = Name) ->
  165. render(Name, []);
  166. render("csstags" = Name) ->
  167. render(Name, []);
  168. render("var_preset" = Name) ->
  169. render(Name, [{var1, "foostring1"}, {var2, "foostring2"}]);
  170. render("for_preset" = Name) ->
  171. render(Name, []);
  172. render("for_records_preset" = Name) ->
  173. Link1 = [{name, "Canon"}, {url, "http://canon.com"}],
  174. Link2 = [{name, "Leica"}, {url, "http://leica.com"}],
  175. Link3 = [{name, "Nikon"}, {url, "http://nikon.com"}],
  176. render(Name, [{photo_links, [Link1, Link2, Link3]}]);
  177. render(Name) ->
  178. io:format("No such template: ~p~n",[Name]).
  179. %%--------------------------------------------------------------------
  180. %% @spec (atom(), string(), string()) -> any()
  181. %% @doc renders template to a file
  182. %% @end
  183. %%--------------------------------------------------------------------
  184. render(Name, Args) ->
  185. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  186. render2(OutDir, list_to_atom("test_" ++ Name), Args).
  187. %%--------------------------------------------------------------------
  188. %% @spec (atom()) -> proplist()
  189. %% @doc returns template preset variables
  190. %% @end
  191. %%--------------------------------------------------------------------
  192. preset(test_extends) ->
  193. [{preset_base_var, "preset-base-barstring"}];
  194. preset(test_var_preset) ->
  195. [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}];
  196. preset(test_for_preset) ->
  197. [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}];
  198. preset(test_for_records_preset) ->
  199. Link1 = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  200. Link2 = [{name, "Google (preset)"}, {url, "http://google.com"}],
  201. Link3 = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  202. [{software_links, [Link1, Link2, Link3]}].
  203. %%====================================================================
  204. %% Internal functions
  205. %%====================================================================
  206. render2(OutDir, Module, Arg) ->
  207. case catch Module:render(Arg) of
  208. {ok, Val, Warnings} ->
  209. write_file(OutDir, Module, Val, Warnings);
  210. {error, Err, Warnings} ->
  211. io:format("TRACE ~p:~p Errors: ~p~n",[?MODULE, ?LINE, Err]),
  212. io:format("TRACE ~p:~p Warnings: ~p~n",[?MODULE, ?LINE, Warnings]);
  213. {'EXIT', Reason} ->
  214. io:format("TRACE ~p:~p ~p: render failure: ~n",[?MODULE, ?LINE, Reason]);
  215. Val -> %% only temporarly
  216. write_file(OutDir, Module, Val, [])
  217. end.
  218. write_file(OutDir, Module, Val, Warnings) ->
  219. case file:open(filename:join([OutDir, lists:concat([Module, ".", Module:file_extension()])]), [write]) of
  220. {ok, IoDev} ->
  221. file:write(IoDev, Val),
  222. file:close(IoDev),
  223. case Warnings of
  224. [] ->
  225. io:format("render success: ~p~n",[Module]);
  226. _ ->
  227. io:format("render success: ~p - Warnings: ~p~n",[Module, Warnings])
  228. end;
  229. _ ->
  230. io:format("render failure: ~p~n",[Module])
  231. end.