Browse Source

Revert to previous compile_dir functionality (fixes #148)

The backward incompatible change introduced in commit
0b61b69591f60a427d65faa95c00119e23edad09 is reverted, and instead a
new `render` function is added for those who wish to get the tagged
result tuple `{ok, iolist()} | {error, Reason}`.
Andreas Stenius 11 years ago
parent
commit
6f5c9e4d5d
4 changed files with 34 additions and 10 deletions
  1. 3 0
      NEWS.md
  2. 6 0
      README.markdown
  3. 22 9
      src/erlydtl_beam_compiler.erl
  4. 3 1
      test/erlydtl_test_defs.erl

+ 3 - 0
NEWS.md

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

+ 6 - 0
README.markdown

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

+ 22 - 9
src/erlydtl_beam_compiler.erl

@@ -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}}
@@ -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)),
@@ -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 }) ->

+ 3 - 1
test/erlydtl_test_defs.erl

@@ -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
                   }]
       ]}
     ].