Erlang Erlydtl从目录中加载模板。

Erlang Erlydtl从目录中加载模板。,erlang,erlydtl,Erlang,Erlydtl,方便地将所有模板放入特殊目录中,并在C.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

方便地将所有模板放入特殊目录中,并在C.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).

exist是使用erlydtl执行此操作的标准方法吗

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

{{ foo }} and {{ foo }}
和模板2.dtl

{{ foo }}
两者都在目录“模板”中。 Compile(注意out\u dir必须存在):

渲染和结果:

templates:render('1',[{foo,"bar"}]).
{ok,["bar",<<" and ">>,"bar",<<"\n">>]}
templates:render('2',[{foo,"bar"}]).
{ok,["bar",<<"\n">>]}
templates:render('1',[{foo,bar}])。
{好的,[“吧台”,“吧台”,“吧台]}
模板:render('2',[{foo,bar}])。
{好的,[“条”,]}
使用
module\u info
函数始终可以获得的所有生成函数的相关信息

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