erlydtl_demo.erl 4.7 KB

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