erlydtl_demo.erl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. render_html/0]).
  38. %%====================================================================
  39. %% API
  40. %%====================================================================
  41. %%--------------------------------------------------------------------
  42. %% @spec () -> any()
  43. %% @doc compiles the templates to beam files
  44. %% @end
  45. %%--------------------------------------------------------------------
  46. compile_templates() ->
  47. DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
  48. filelib:fold_files(DocRoot,
  49. "\.",
  50. true,
  51. fun(Path, _Acc) ->
  52. Name = filename:rootname(filename:basename(Path)),
  53. erlydtl_api:compile(Path, Name, Name, DocRoot)
  54. end,
  55. []).
  56. %%--------------------------------------------------------------------
  57. %% @spec () -> any()
  58. %% @doc renders the templete to a file
  59. %% @end
  60. %%--------------------------------------------------------------------
  61. render_html() ->
  62. OutDir = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "out"]),
  63. render(OutDir, test_variable, ".html", "foostring"),
  64. render(OutDir, test_extend, ".html", "blastring"),
  65. render(OutDir, test_comment, ".html").
  66. %%====================================================================
  67. %% Internal functions
  68. %%====================================================================
  69. render(OutDir, Name, Ext, Var) ->
  70. case catch Name:Name(Var) of
  71. {'EXIT', Reason} ->
  72. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  73. Val ->
  74. case file:open(filename:join([OutDir, lists:concat([Name, Ext])]), [write]) of
  75. {ok, IoDev} ->
  76. file:write(IoDev, Val),
  77. file:close(IoDev),
  78. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Name]);
  79. _ ->
  80. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Name])
  81. end
  82. end.
  83. render(OutDir, Name, Ext) ->
  84. case catch Name:Name() of
  85. {'EXIT', Reason} ->
  86. io:format("TRACE ~p:~p ~p: rendering failure: ~n",[?MODULE, ?LINE, Reason]);
  87. Val ->
  88. case file:open(filename:join([OutDir, lists:concat([Name, Ext])]), [write]) of
  89. {ok, IoDev} ->
  90. file:write(IoDev, Val),
  91. file:close(IoDev),
  92. io:format("TRACE ~p:~p ~p: success~n",[?MODULE, ?LINE, Name]);
  93. _ ->
  94. io:format("TRACE ~p:~p ~p: file write failure~n",[?MODULE, ?LINE, Name])
  95. end
  96. end.