erlydtl_demo.erl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. filelib:fold_files(DocRoot,
  48. "\.html$|\.css$",
  49. true,
  50. fun(Path, _Acc) ->
  51. Module = filename:rootname(filename:basename(Path)),
  52. % case erlydtl_server:compile(Path, DocRoot, Module, {?MODULE, preset}) of
  53. case erlydtl_compiler:compile(Path, DocRoot, Module) of
  54. ok ->
  55. io:format("compile success: ~p~n",[Module]);
  56. _ ->
  57. io:format("compile failure: ~p~n",[Module])
  58. end
  59. end,
  60. []).
  61. %%--------------------------------------------------------------------
  62. %% @spec (string()) -> any()
  63. %% @doc
  64. %% compiles the template to beam files
  65. %% @end
  66. %%--------------------------------------------------------------------
  67. compile("var" = Name) ->
  68. compile(Name, ".html");
  69. compile("extends" = Name) ->
  70. compile(Name, ".html");
  71. compile("comment" = Name) ->
  72. compile(Name, ".html");
  73. compile("for" = Name) ->
  74. compile(Name, ".html");
  75. compile("for_records" = Name) ->
  76. compile(Name, ".html");
  77. compile("htmltags" = Name) ->
  78. compile(Name, ".html");
  79. compile("csstags" = Name) ->
  80. compile(Name, ".css");
  81. compile("var_preset" = Name) ->
  82. compile(Name, ".html");
  83. compile("for_preset" = Name) ->
  84. compile(Name, ".html");
  85. compile("for_records_preset" = Name) ->
  86. compile(Name, ".html");
  87. compile(Name) ->
  88. io:format("No such template: ~p~n",[Name]).
  89. %%--------------------------------------------------------------------
  90. %% @spec (string(), string()) -> any()
  91. %% @doc
  92. %% compiles the template to beam files
  93. %% @end
  94. %%--------------------------------------------------------------------
  95. compile(Name, Ext) ->
  96. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  97. Module = "test_" ++ Name,
  98. Path = filename:join([DocRoot, Module ++ Ext]),
  99. % case erlydtl_server:compile(Path, DocRoot, Module, {?MODULE, preset}) of
  100. case erlydtl_compiler:compile(Path, DocRoot, Module) of
  101. ok ->
  102. io:format("compile success: ~p~n",[Module]);
  103. _ ->
  104. io:format("compile failure: ~p~n",[Module])
  105. end.
  106. %%--------------------------------------------------------------------
  107. %% @spec () -> any()
  108. %% @doc renders template to a file
  109. %% @end
  110. %%--------------------------------------------------------------------
  111. render_all() ->
  112. render("var"),
  113. render("extends"),
  114. render("comment"),
  115. render("for"),
  116. render("for_records"),
  117. render("htmltags"),
  118. render("csstags"),
  119. render("var_preset"),
  120. render("for_preset"),
  121. render("for_records_preset").
  122. %%--------------------------------------------------------------------
  123. %% @spec (string()) -> ok()
  124. %% @doc renders template to a file
  125. %% @end
  126. %%--------------------------------------------------------------------
  127. render("var" = Name) ->
  128. render(Name, ".html", [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}]);
  129. render("extends" = Name) ->
  130. render(Name, ".html", [{base_var, "base-barstring"}, {test_var, "test-barstring"}]);
  131. render("comment" = Name) ->
  132. render(Name, ".html");
  133. render("for" = Name) ->
  134. render(Name, ".html", [{fruit_list, ["apple", "banana", "coconut"]}]);
  135. render("for_records" = Name) ->
  136. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  137. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  138. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  139. render(Name, ".html", [{link_list, [Link1, Link2, Link3]}]);
  140. render("htmltags" = Name) ->
  141. render(Name, ".html");
  142. render("csstags" = Name) ->
  143. render(Name, ".css");
  144. render("var_preset" = Name) ->
  145. render(Name, ".html", [{var1, "foostring1"}, {var2, "foostring2"}]);
  146. render("for_preset" = Name) ->
  147. render(Name, ".html");
  148. render("for_records_preset" = Name) ->
  149. Link1 = [{name, "Canon"}, {url, "http://canon.com"}],
  150. Link2 = [{name, "Leica"}, {url, "http://leica.com"}],
  151. Link3 = [{name, "Nikon"}, {url, "http://nikon.com"}],
  152. render(Name, ".html", [{photo_links, [Link1, Link2, Link3]}]);
  153. render(Name) ->
  154. io:format("No such template: ~p~n",[Name]).
  155. %%--------------------------------------------------------------------
  156. %% @spec (atom(), string()) -> any()
  157. %% @doc renders template to a file
  158. %% @end
  159. %%--------------------------------------------------------------------
  160. render(Name, Ext) ->
  161. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  162. render2(OutDir, list_to_atom("test_" ++ Name), Ext).
  163. %%--------------------------------------------------------------------
  164. %% @spec (atom(), string(), string()) -> any()
  165. %% @doc renders template to a file
  166. %% @end
  167. %%--------------------------------------------------------------------
  168. render(Name, Ext, Args) ->
  169. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  170. render2(OutDir, list_to_atom("test_" ++ Name), Ext, Args).
  171. %%--------------------------------------------------------------------
  172. %% @spec (atom()) -> proplist()
  173. %% @doc returns template preset variables
  174. %% @end
  175. %%--------------------------------------------------------------------
  176. preset(test_extends) ->
  177. [{preset_base_var, "preset-base-barstring"}];
  178. preset(test_var_preset) ->
  179. [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}];
  180. preset(test_for_preset) ->
  181. [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}];
  182. preset(test_for_records_preset) ->
  183. Link1 = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  184. Link2 = [{name, "Google (preset)"}, {url, "http://google.com"}],
  185. Link3 = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  186. [{software_links, [Link1, Link2, Link3]}].
  187. %%====================================================================
  188. %% Internal functions
  189. %%====================================================================
  190. render2(OutDir, Module, Ext, Arg) ->
  191. case catch apply(Module, render, [Arg]) of
  192. {ok, Val, Warnings} ->
  193. write_file(OutDir, Module, Ext, Val, Warnings);
  194. {error, Err, Warnings} ->
  195. io:format("TRACE ~p:~p Errors: ~p~n",[?MODULE, ?LINE, Err]),
  196. io:format("TRACE ~p:~p Warnings: ~p~n",[?MODULE, ?LINE, Warnings]);
  197. {'EXIT', Reason} ->
  198. io:format("TRACE ~p:~p ~p: render failure: ~n",[?MODULE, ?LINE, Reason]);
  199. Val -> %% only temporarly
  200. write_file(OutDir, Module, Ext, Val, [])
  201. end.
  202. render2(OutDir, Module, Ext) ->
  203. case catch Module:render() of
  204. {ok, Val, Warnings} ->
  205. write_file(OutDir, Module, Ext, Val, Warnings);
  206. {error, Err, Warnings} ->
  207. io:format("TRACE ~p:~p Errors: ~p~n",[?MODULE, ?LINE, Err]),
  208. io:format("TRACE ~p:~p Warnings: ~p~n",[?MODULE, ?LINE, Warnings]);
  209. {'EXIT', Reason} ->
  210. io:format("TRACE ~p:~p ~p: render failure: ~n",[?MODULE, ?LINE, Reason]);
  211. Val -> %% only temporarly
  212. write_file(OutDir, Module, Ext, Val, [])
  213. end.
  214. write_file(OutDir, Module, Ext, Val, Warnings) ->
  215. case file:open(filename:join([OutDir, lists:concat([Module, Ext])]), [write]) of
  216. {ok, IoDev} ->
  217. file:write(IoDev, Val),
  218. file:close(IoDev),
  219. case Warnings of
  220. [] ->
  221. io:format("render success: ~p~n",[Module]);
  222. _ ->
  223. io:format("render success: ~p - Warnings: ~p~n",[Module, Warnings])
  224. end;
  225. _ ->
  226. io:format("render failure: ~p~n",[Module])
  227. end.