PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

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