Erlydtl从目录加载模板。

时间:2017-01-28 20:18:36

标签: erlang erlydtl

将所有模板放入特殊目录并编译onсe。现在我写了自己的函数,例如:

make_templates(Dir)->
    Files = filelib:wildcard(Dir++"/*.dtl"),
    compile(Files,0).

compile([],_)->ok;
compile([File|T],N)->
    io:format("Compile file ~p~n",[File]),
    R=erlydtl:compile_file(
        File,
        "template_"++integer_to_list(N),
        [{out_dir},"templates"]),
    compile(T,N+1).

使用erlydtl存在标准方法吗?

1 个答案:

答案 0 :(得分:0)

是。为此目的,存在函数erlydtl:compile_dir/2erlydtl:compile_dir/3。结果模块具有模板的名称。 例如,您有模板1.dtl

{{ foo }} and {{ foo }}

和模板2.dtl

{{ foo }}

目录中的“模板”。 编译(注意out_dir必须存在):

erlydtl:compile_dir("templates", templates, [{out_dir,"ebin"}]).

渲染和结果:

templates:render('1',[{foo,"bar"}]).
{ok,["bar",<<" and ">>,"bar",<<"\n">>]}
templates:render('2',[{foo,"bar"}]).
{ok,["bar",<<"\n">>]}

有关module_info函数始终可以获得的所有生成函数的信息。