2017-01-28 4 views
0

便利なことに、すべてのテンプレートを特別なディレクトリに配置し、onkeをコンパイルします。今私は自分自身の機能を叱った:Erlydtlディレクトリからテンプレートをロードしています。

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でこれを行うための標準的な方法はありますか?

答えて

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機能を得ることができ、生成されたすべての機能についての情報を。

関連する問題