erlydtl_demo.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_templates/0,
  37. compile_test_template/1,
  38. compile_test_template/2,
  39. render_all/0]).
  40. %%====================================================================
  41. %% API
  42. %%====================================================================
  43. %%--------------------------------------------------------------------
  44. %% @spec () -> any()
  45. %% @doc compiles the templates to beam files
  46. %% @end
  47. %%--------------------------------------------------------------------
  48. compile_templates() ->
  49. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  50. filelib:fold_files(DocRoot,
  51. "\.html$|\.css$",
  52. true,
  53. fun(Path, _Acc) ->
  54. Name = filename:rootname(filename:basename(Path)),
  55. erlydtl_server:compile(Path, Name, DocRoot)
  56. end,
  57. []).
  58. %%--------------------------------------------------------------------
  59. %% @spec (string()) -> any()
  60. %% @doc
  61. %% compiles the template to beam files
  62. %% @end
  63. %%--------------------------------------------------------------------
  64. compile_test_template(Name) ->
  65. compile_test_template(Name, ".html").
  66. %%--------------------------------------------------------------------
  67. %% @spec (string(), string()) -> any()
  68. %% @doc
  69. %% compiles the template to beam files
  70. %% @end
  71. %%--------------------------------------------------------------------
  72. compile_test_template(Name, Ext) ->
  73. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  74. Name2 = "test_" ++ Name,
  75. Path = filename:join([DocRoot, Name2 ++ Ext]),
  76. erlydtl_server:compile(Path, Name2, DocRoot).
  77. %%--------------------------------------------------------------------
  78. %% @spec () -> any()
  79. %% @doc renders the templete to a file
  80. %% @end
  81. %%--------------------------------------------------------------------
  82. render_all() ->
  83. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  84. render(OutDir, test_variable, ".html", ["foostring"]),
  85. render(OutDir, test_extend, ".html", ["bar1string", "bar2string"]),
  86. render(OutDir, test_comment, ".html"),
  87. render(OutDir, test_tags, ".html"),
  88. render(OutDir, test_tags, ".css").
  89. %%====================================================================
  90. %% Internal functions
  91. %%====================================================================
  92. render(OutDir, Module, Ext, Args) ->
  93. case catch apply(Module, render, Args) of
  94. {'EXIT', Reason} ->
  95. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  96. Val ->
  97. case file:open(filename:join([OutDir, lists:concat([Module, Ext])]), [write]) of
  98. {ok, IoDev} ->
  99. file:write(IoDev, Val),
  100. file:close(IoDev),
  101. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Module]);
  102. _ ->
  103. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Module])
  104. end
  105. end.
  106. render(OutDir, Module, Ext) ->
  107. case catch Module:render() of
  108. {'EXIT', Reason} ->
  109. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  110. Val ->
  111. case file:open(filename:join([OutDir, lists:concat([Module, Ext])]), [write]) of
  112. {ok, IoDev} ->
  113. file:write(IoDev, Val),
  114. file:close(IoDev),
  115. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Module]);
  116. _ ->
  117. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Module])
  118. end
  119. end.