/modules/mod_base/scomps/scomp_base_include.erl

https://code.google.com/p/zotonic/ · Erlang · 54 lines · 25 code · 8 blank · 21 comment · 1 complexity · db6bb04e38a59f34618ff77768ea5c24 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009-2011 Marc Worrell
  3. %% @doc Include a template, with possible caching
  4. %%
  5. %% Example: include "some_file.tpl" and cache it for 3600 seconds
  6. %% {% include vary="something" maxage=3600 file="some_file.tpl" %}
  7. %%
  8. %% Give a maxage of 0 for slam dunk protection but no caching.
  9. %% Copyright 2009-2011 Marc Worrell
  10. %%
  11. %% Licensed under the Apache License, Version 2.0 (the "License");
  12. %% you may not use this file except in compliance with the License.
  13. %% You may obtain a copy of the License at
  14. %%
  15. %% http://www.apache.org/licenses/LICENSE-2.0
  16. %%
  17. %% Unless required by applicable law or agreed to in writing, software
  18. %% distributed under the License is distributed on an "AS IS" BASIS,
  19. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. %% See the License for the specific language governing permissions and
  21. %% limitations under the License.
  22. -module(scomp_base_include).
  23. -behaviour(gen_scomp).
  24. -export([vary/2, render/3]).
  25. -include("zotonic.hrl").
  26. vary(_Params, _Context) -> default.
  27. render(Params, Vars, Context) ->
  28. File = proplists:get_value('$file', Params),
  29. AddC = fun
  30. ({Name,Value}, Vs) when Name =/= '$file' andalso Name =/= vary andalso Name =/= maxage ->
  31. [{Name,Value}|Vs];
  32. (_, Vs) ->
  33. Vs
  34. end,
  35. Vars1 = lists:foldl(AddC, Vars, Params),
  36. Context1 = case proplists:get_value(sudo, Params) of
  37. true -> z_acl:sudo(Context);
  38. _ -> Context
  39. end,
  40. case proplists:get_value('$all', Params, false) of
  41. false ->
  42. {ok, z_template:render(File, Vars1, Context1)};
  43. true ->
  44. Templates = z_template:find_template(File, true, Context1),
  45. {ok, [ z_template:render(Tpl, Vars1, Context1) || Tpl <- Templates ]}
  46. end.