Skip to content

Commit

Permalink
Revert to previous compile_dir functionality (fixes #148)
Browse files Browse the repository at this point in the history
The backward incompatible change introduced in commit
0b61b69 is reverted, and instead a
new `render` function is added for those who wish to get the tagged
result tuple `{ok, iolist()} | {error, Reas}`.
  • Loading branch information
kaos committed Mar 11, 2014
1 parent fd79c34 commit 73bd19d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -8,6 +8,9 @@ Standards](http://www.gnu.org/prep/standards/html_node/NEWS-File.html#NEWS-File)

* Added NEWS file.
* Fixed broken `compile_dir` (#146).
* The backwards incompatible change in 0.9.1 for directory-compiled
templates has been reverted. A new `render` function has been added
instead (#148).


## 0.9.1 (2014-03-02)
Expand Down
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -248,6 +248,12 @@ Compiling a helper module can be more efficient than using
`custom_tags_dir` because the helper functions will be compiled only
once (rather than once per template).
Notice: The exported template functions return an `iolist()` on
success only, failures are non-local (e.g. as a throw). To get the
result in wrapped tuple `{ok, iolist()} | {error, Reason}` call one of
the `render` functions: `render(Tag) | render(Tag, Vars) | render(Tag,
Vars, Opts)`.
Usage (of a compiled template)
------------------------------
Expand Down
31 changes: 22 additions & 9 deletions src/erlydtl_beam_compiler.erl
Expand Up @@ -163,11 +163,7 @@ compile_multiple_to_binary(Dir, ParserResults, Context) ->
FunctionName = filename:rootname(filename:basename(File)),
FunctionDefs = ?Q(["'@func'(Variables) -> _@func(Variables, []).",
"'@func'(_Variables, RenderOptions) ->",
" try _@MatchAst, _@body of",
" Val -> {ok, Val}",
" catch",
" Err -> {error, Err}",
" end."],
" _@MatchAst, _@body."],
[{func, erl_syntax:atom(FunctionName)},
{body, stringify(BodyAst, Ctx)}]),
{{FunctionName, FunctionDefs}, {merge_info(AstInfo, BodyInfo), TreeWalker1}}
Expand Down Expand Up @@ -436,7 +432,10 @@ variables_function(Variables) ->
custom_forms(Dir, Module, Functions, AstInfo) ->
Exported = [erl_syntax:arity_qualifier(erl_syntax:atom(source_dir), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(dependencies), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(translatable_strings), erl_syntax:integer(0))
erl_syntax:arity_qualifier(erl_syntax:atom(translatable_strings), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(render), erl_syntax:integer(1)),
erl_syntax:arity_qualifier(erl_syntax:atom(render), erl_syntax:integer(2)),
erl_syntax:arity_qualifier(erl_syntax:atom(render), erl_syntax:integer(3))
| lists:foldl(
fun({FunctionName, _}, Acc) ->
[erl_syntax:arity_qualifier(erl_syntax:atom(FunctionName), erl_syntax:integer(1)),
Expand All @@ -449,13 +448,27 @@ custom_forms(Dir, Module, Functions, AstInfo) ->

SourceFunctionAst = ?Q("source_dir() -> _@Dir@."),

RenderAsts = ?Q(["render(Tag) -> render(Tag, [], []).",
"render(Tag, Vars) -> render(Tag, Vars, []).",
"render(Tag, Vars, Opts) ->",
" try '@Module@':Tag(Vars, Opts) of",
" Val -> {ok, Val}",
" catch",
" Err -> {error, Err}",
" end."]),

DependenciesFunctionAst = dependencies_function(AstInfo#ast_info.dependencies),
TranslatableStringsFunctionAst = translatable_strings_function(AstInfo#ast_info.translatable_strings),
FunctionAsts = lists:foldl(fun({_, FunctionDefs}, Acc) -> FunctionDefs ++ Acc end, [], Functions),
FunctionAsts = lists:foldl(
fun({_, FunctionDefs}, Acc) ->
FunctionDefs ++ Acc
end,
RenderAsts, Functions),

[erl_syntax:revert(X)
|| X <- [ModuleAst, ExportAst, SourceFunctionAst, DependenciesFunctionAst, TranslatableStringsFunctionAst
| FunctionAsts] ++ AstInfo#ast_info.pre_render_asts
|| X <- [ModuleAst, ExportAst, SourceFunctionAst, DependenciesFunctionAst,
TranslatableStringsFunctionAst | FunctionAsts]
++ AstInfo#ast_info.pre_render_asts
].

stringify(BodyAst, #dtl_context{ binary_strings=BinaryStrings }) ->
Expand Down
4 changes: 3 additions & 1 deletion test/erlydtl_test_defs.erl
Expand Up @@ -1434,7 +1434,9 @@ all_test_defs() ->
#test{
title = "path1",
source = {dir, template_file(input, "path1")},
renderer = base1
renderer = fun(#test{ module=M, render_vars=V, render_opts=O }) ->
M:render(base1, V, O)
end
}]
]}
].
Expand Down

0 comments on commit 73bd19d

Please sign in to comment.