erlydtl_demo.erl 8.3 KB

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