erlydtl/erlydtl — pull request #158 by Dryymoon (2014-04-01 08:48:52+00:00)
Title: Proposition to improvement

erlydtl_runtime.erl rows 40-100

Change core to this, because without changes if some value in list or dict or else erlydtl cant find - it will fail and dont render template.

find_value(Key, List)->
try
find_value_int(Key, List)
catch
_:_ -> undefined
end.

find_value_int(_, undefined) ->
undefined;
find_value_int(Key, Fun) when is_function(Fun, 1) ->
Fun(Key);
find_value_int(Key, L) when is_atom(Key), is_list(L) ->
case lists:keyfind(Key, 1, L) of
false -> find_value(atom_to_list(Key), L);
{Key, Value} -> Value
end;
find_value_int(Key, L) when is_list(Key), is_list(L) ->
case lists:keyfind(Key, 1, L) of
false -> find_value(list_to_binary(Key), L);
{Key, Value} -> Value
end;
find_value_int(Key, L) when is_binary(Key), is_list(L) ->
case lists:keyfind(Key, 1, L) of
false -> undefined;
{Key, Value} -> Value
end;
find_value_int(Key, L) when is_integer(Key), is_list(L) ->
if Key =< length(L) -> lists:nth(Key, L);
true -> undefined
end;
find_value_int(Key, {GBSize, GBData}) when is_integer(GBSize) ->
case gb_trees:lookup(Key, {GBSize, GBData}) of
{value, Val} ->
Val;
_ ->
undefined
end;
find_value_int(Key, Tuple) when is_tuple(Tuple) ->
case element(1, Tuple) of
dict ->
case dict:find(Key, Tuple) of
{ok, Val} ->
Val;
_ ->
undefined
end;
_ when is_integer(Key) ->
if Key =< size(Tuple) -> element(Key, Tuple);
true -> undefined
end;
Module ->
case lists:member({Key, 1}, Module:module_info(exports)) of
true ->
case Tuple:Key() of
Val when is_tuple(Val) ->
case element(1, Val) of
'Elixir.Ecto.Associations.BelongsTo' -> Val:get();
'Elixir.Ecto.Associations.HasOne' -> Val:get();
_ -> Val
end;
Val -> Val
end;
_ ->
undefined
end
end.

erlydtl_runtime.erl row 258-280
Change core to this, because erlydtl cant work with float values properly.

list_to_num(Bin) ->
%% N = binary_to_list(Bin),
case string:to_float(Bin) of
{error,no_float} -> list_to_integer(Bin);
{F,_Rest} -> F
end.

'gt'(Value1, Value2) when is_list(Value1) ->
'gt'(list_to_num(Value1), Value2);
'gt'(Value1, Value2) when is_list(Value2) ->
'gt'(Value1, list_to_num(Value2));
'gt'(Value1, Value2) when Value1 > Value2 ->
true;
'gt'(_, _) ->
false.

'lt'(Value1, Value2) when is_list(Value1) ->
'lt'(list_to_num(Value1), Value2);
'lt'(Value1, Value2) when is_list(Value2) ->
'lt'(Value1, list_to_num(Value2));
'lt'(Value1, Value2) when Value1 < Value2 ->
true;
'lt'(_, _) ->
false.