Browse Source

Use yesno filter with lists and binaries

This comes by default with Python as empty lists evaluate to false. It's
explicitly implemented as a term_to_bool function in filters.
Garrett Smith 12 years ago
parent
commit
a46a2d2ed5
1 changed files with 9 additions and 2 deletions
  1. 9 2
      src/erlydtl_filters.erl

+ 9 - 2
src/erlydtl_filters.erl

@@ -1203,8 +1203,8 @@ process_binary_match(Pre, Insertion, SizePost, Post) ->
         _ -> [Pre, Insertion, Post]
         _ -> [Pre, Insertion, Post]
     end.
     end.
 
 
-yesno_io(Bool, Choices) ->
-    case {Bool, binary:split(Choices, <<",">>, [global])} of
+yesno_io(Val, Choices) ->
+    case {term_to_bool(Val), binary:split(Choices, <<",">>, [global])} of
         {true, [T|_]} -> T;
         {true, [T|_]} -> T;
         {false, [_,F|_]} -> F;
         {false, [_,F|_]} -> F;
         {undefined, [_,_,U|_]} -> U;
         {undefined, [_,_,U|_]} -> U;
@@ -1212,6 +1212,13 @@ yesno_io(Bool, Choices) ->
         _ -> error
         _ -> error
     end.
     end.
 
 
+term_to_bool(true) -> true;
+term_to_bool(false) -> false;
+term_to_bool(undefined) -> undefined;
+term_to_bool(Str) when is_list(Str); is_binary(Str) ->
+    iolist_size(Str) > 0;
+term_to_bool(_) -> true.
+
 %% unjoin == split in other languages; inverse of join
 %% unjoin == split in other languages; inverse of join
 %%FROM: http://www.erlang.org/pipermail/erlang-questions/2008-October/038896.html
 %%FROM: http://www.erlang.org/pipermail/erlang-questions/2008-October/038896.html
 unjoin(String, []) ->
 unjoin(String, []) ->