Anonymous avatar Anonymous committed 89d3d53

Issue #6

Applied patch from Dan Milstein to fix a crash when rendering floats.

Added a baseline failing test case for the problem.

Comments (0)

Files changed (3)

src/erlydtl/erlydtl_compiler.erl

         end, undefined, Context#dtl_context.local_scopes).
 
 format(Ast, Context) ->
-    auto_escape(format_integer_ast(Ast), Context).
+    auto_escape(format_number_ast(Ast), Context).
 
 
-format_integer_ast(Ast) ->
-    erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(format_integer),
+format_number_ast(Ast) ->
+    erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(format_number),
         [Ast]).
 
 

src/erlydtl/erlydtl_filters.erl

 format_integer(Input) ->
     Input.
 
+format_number(Input) when is_integer(Input) ->
+    integer_to_list(Input);
+format_number(Input) when is_float(Input) ->
+    io_lib:format("~p", [Input]);
+format_number(Input) ->
+    Input.
+
 join([Input], Separator) when is_list(Input) ->
     string:join(Input, Separator).
 

src/tests/erlydtl_unittests.erl

 
 tests() ->
     [
+        {"vars", [
+                {"string",
+                    <<"String value is: {{ var1 }}">>,
+                    [{var1, "foo"}], <<"String value is: foo">>},
+                {"int",
+                    <<"The magic number is: {{ var1 }}">>,
+                    [{var1, 42}], <<"The magic number is: 42">>},
+                {"float",
+                    <<"The price of milk is: {{ var1 }}">>,
+                    [{var1, 0.42}], <<"The price of milk is: 0.42">>}
+            ]},
         {"comment", [
-                {"Comment block is excised",
-                    <<"Bob {% comment %}(moron){% endcomment %} Loblaw">>,
-                    [], <<"Bob  Loblaw">>},
-                {"Inline comment is excised",
-                    <<"You're {# not #} a very nice person">>,
-                    [], <<"You're  a very nice person">>}
+                {"comment block is excised",
+                    <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
+                    [], <<"bob  loblaw">>},
+                {"inline comment is excised",
+                    <<"you're {# not #} a very nice person">>,
+                    [], <<"you're  a very nice person">>}
             ]},
         {"autoescape", [
                 {"Autoescape works",
                     <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
                 {"|format_integer",
                     <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
+                {"|format_number 1",
+                    <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
+                {"|format_number 2",
+                    <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
+                {"|format_number 3",
+                    <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
+                {"|format_number 4",
+                    <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
+ 
                 {"|join:\", \"",
                     <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
                     <<"Liberte, Egalite, Fraternite">>},
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.