Permalink
Browse files

Render from function of arity-1.

The variable name is passed to the function as an atom. Thanks to noss.
  • Loading branch information...
1 parent f4266d2 commit 449f2e77aff3c99fe541489e60be6083526bd967 Evan Miller committed Apr 26, 2010
Showing with 7 additions and 1 deletion.
  1. +3 −1 src/erlydtl/erlydtl_runtime.erl
  2. +4 −0 src/tests/erlydtl_unittests.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 ->
@@ -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",

0 comments on commit 449f2e7

Please sign in to comment.