%%% BEGIN openflax/handler/template.erl %%% %%% %%% openflax - Open Source web server for Erlang/OTP %%% Copyright (c)2003 Cat's Eye Technologies. All rights reserved. %%% %%% Redistribution and use in source and binary forms, with or without %%% modification, are permitted provided that the following conditions %%% are met: %%% %%% Redistributions of source code must retain the above copyright %%% notice, this list of conditions and the following disclaimer. %%% %%% Redistributions in binary form must reproduce the above copyright %%% notice, this list of conditions and the following disclaimer in %%% the documentation and/or other materials provided with the %%% distribution. %%% %%% Neither the name of Cat's Eye Technologies nor the names of its %%% contributors may be used to endorse or promote products derived %%% from this software without specific prior written permission. %%% %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND %%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, %%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF %%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE %%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE %%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, %%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, %%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON %%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, %%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY %%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE %%% POSSIBILITY OF SUCH DAMAGE. %% @doc Simple template generating handler for OpenFlax. %% %%
Typically this handler is swapped to from another handler. %% This handler doesn't care about the basic resource or document, %% and only uses the configuration settings given to it %% to generate a simple template.
%% %% @end -module(openflax.handler.template). -vsn('$Id: template.erl 31 2004-04-23 07:00:11Z catseye $'). -author('cpressey@catseye.mine.nu'). -copyright('Copyright (c)2003 Cat`s Eye Technologies. All rights reserved.'). -behaviour(openflax.handler). -export([start/1, stop/1, serve/1]). % behaviour -export([fill_out/2]). -import(string). %% @spec start(conf()) -> conf() %% @doc Initializes the templating handler. start(Conf) -> openflax.conf:new(). %% @spec stop(conf()) -> conf() %% @doc Shuts down the templating handler. stop(Conf) -> Conf. %% @spec serve(conf()) -> {response(), conf()} %% @doc Serves a template to the user agent. serve(Conf) -> Body = openflax.conf:get_string(cfg_template_body, Conf), Conf0 = openflax.conf:put_value(res_content_type, "text/html", Conf), {{content, fill_out(Body, Conf0)}, Conf0}. %% @spec fill_out(string(), conf()) -> string() %% @doc Replaces all${tag}s in the given text
%% with values from the dictionary. The returned list is not flat.
fill_out(Text, Conf) ->
fill_out(Text, Conf, "").
fill_out(Text, Conf, Acc) ->
case string:str(Text, "${") of
0 ->
[Acc | Text];
N ->
M = string:chr(Text, $}),
Pre = string:substr(Text, 1, N - 1),
Key = list_to_atom(string:substr(Text, N + 2, M - (N + 2))),
Post = string:substr(Text, M + 1),
Value = openflax.conf:get_string(Key, Conf),
fill_out(Post, Conf, [Acc, Pre | Value])
end.
%%% END of openflax/handler/template.erl %%%