erlydtl_demo.erl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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]).
  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. Name = filename:rootname(filename:basename(Path)),
  52. erlydtl_server:compile(Path, Name, DocRoot)
  53. end,
  54. []).
  55. %%--------------------------------------------------------------------
  56. %% @spec (string()) -> any()
  57. %% @doc
  58. %% compiles the template to beam files
  59. %% @end
  60. %%--------------------------------------------------------------------
  61. compile(Name) ->
  62. compile(Name, ".html").
  63. %%--------------------------------------------------------------------
  64. %% @spec (string(), string()) -> any()
  65. %% @doc
  66. %% compiles the template to beam files
  67. %% @end
  68. %%--------------------------------------------------------------------
  69. compile(Name, Ext) ->
  70. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  71. Name2 = "test_" ++ Name,
  72. Path = filename:join([DocRoot, Name2 ++ Ext]),
  73. erlydtl_server:compile(Path, Name2, DocRoot).
  74. %%--------------------------------------------------------------------
  75. %% @spec () -> any()
  76. %% @doc renders template to a file
  77. %% @end
  78. %%--------------------------------------------------------------------
  79. render() ->
  80. render("variable", ".html", ["foostring", "bar"]),
  81. render("extends", ".html", ["bar1string", "bar2string"]),
  82. render("comment", ".html"),
  83. render("simple", ".html"),
  84. render("htmltags", ".html"),
  85. render("csstags", ".css"),
  86. render("for", ".html", [["apple", "banana"]]).
  87. %%--------------------------------------------------------------------
  88. %% @spec (string()) -> ok()
  89. %% @doc renders template to a file
  90. %% @end
  91. %%--------------------------------------------------------------------
  92. render("variable" = Name) ->
  93. render(Name, ".html", ["foostring", "bar"]);
  94. render("extends" = Name) ->
  95. render(Name, ".html", ["bar1string", "bar2string"]);
  96. render("comment" = Name) ->
  97. render(Name, ".html");
  98. render("simple" = Name) ->
  99. render(Name, ".html");
  100. render("htmltags" = Name) ->
  101. render(Name, ".html");
  102. render("csstags" = Name) ->
  103. render(Name, ".html");
  104. render("for" = Name) ->
  105. render(Name, ".html", [["apple", "banana"]]).
  106. %%--------------------------------------------------------------------
  107. %% @spec (atom(), string()) -> any()
  108. %% @doc renders template to a file
  109. %% @end
  110. %%--------------------------------------------------------------------
  111. render(Name, Ext) ->
  112. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  113. render2(OutDir, list_to_atom("test_" ++ Name), Ext).
  114. %%--------------------------------------------------------------------
  115. %% @spec (atom(), string(), string()) -> any()
  116. %% @doc renders template to a file
  117. %% @end
  118. %%--------------------------------------------------------------------
  119. render(Name, Ext, Args) ->
  120. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  121. render2(OutDir, list_to_atom("test_" ++ Name), Ext, Args).
  122. %%====================================================================
  123. %% Internal functions
  124. %%====================================================================
  125. render2(OutDir, Module, Ext, Args) ->
  126. case catch apply(Module, render, Args) of
  127. {'EXIT', Reason} ->
  128. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  129. Val ->
  130. case file:open(filename:join([OutDir, lists:concat([Module, Ext])]), [write]) of
  131. {ok, IoDev} ->
  132. file:write(IoDev, Val),
  133. file:close(IoDev),
  134. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Module]);
  135. _ ->
  136. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Module])
  137. end
  138. end.
  139. render2(OutDir, Module, Ext) ->
  140. case catch Module:render() of
  141. {'EXIT', Reason} ->
  142. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  143. Val ->
  144. case file:open(filename:join([OutDir, lists:concat([Module, Ext])]), [write]) of
  145. {ok, IoDev} ->
  146. file:write(IoDev, Val),
  147. file:close(IoDev),
  148. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Module]);
  149. _ ->
  150. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Module])
  151. end
  152. end.