0

方便地将所有模板放入特殊目录并编译一次。现在我编写了自己的函数,例如:

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 执行此操作的标准方法?

4

1 回答 1

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

于 2017-01-28T20:18:35.757 回答