Browse Source

Render from function of arity-1.

The variable name is passed to the function as an atom. Thanks to noss.
Evan Miller 15 years ago
parent
commit
449f2e77af
2 changed files with 7 additions and 1 deletions
  1. 3 1
      src/erlydtl/erlydtl_runtime.erl
  2. 4 0
      src/tests/erlydtl_unittests.erl

+ 3 - 1
src/erlydtl/erlydtl_runtime.erl

@@ -3,7 +3,9 @@
 -compile(export_all).
 
 find_value(_, undefined) ->
-	undefined;
+    undefined;
+find_value(Key, Fun) when is_function(Fun, 1) ->
+    Fun(Key);
 find_value(Key, L) when is_list(L) ->
     case proplists:get_value(Key, L) of
         undefined ->

+ 4 - 0
src/tests/erlydtl_unittests.erl

@@ -66,10 +66,14 @@ tests() ->
                     <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
                 {"Render variable in gb_tree",
                     <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
+                {"Render variable in arity-1 func",
+                    <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
                 {"Render variable with attribute in dict",
                     <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
                 {"Render variable with attribute in gb_tree",
                     <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
+                {"Render variable with attribute in arity-1 func",
+                    <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
                 {"Render variable in parameterized module",
                     <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
                 {"Nested attributes",