[英]Erlydtl Loading templates from directory.
方便地將所有模板放在特殊目錄中,然后進行編譯。 現在,我編寫了自己的函數,例如:
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存在這樣做的標准方法嗎?
是。 為此,存在函數erlydtl:compile_dir/2
和erlydtl: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
函數獲得有關所有已生成函數的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.