0

Conviently place all templates into special directory and compile onсe. Now I writed own functions, for example so:

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).

Is exist standard way for do this with erlydtl?

1 Answers1

0

Yes. For this purpose exist functions erlydtl:compile_dir/2 and erlydtl:compile_dir/3. Resulted modules have name of your templates. For example you have template 1.dtl

{{ foo }} and {{ foo }}

and template 2.dtl

{{ foo }}

both in directory "templates". Compile(note that the out_dir must be exist):

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

Render and result:

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

Information about all generated function you always can get with module_info function.