/modules/mod_base/filters/filter_member.erl

https://code.google.com/p/zotonic/ · Erlang · 41 lines · 19 code · 6 blank · 16 comment · 0 complexity · 9662c85fd17fda12fb92b19ee058cb87 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2010 Marc Worrell
  3. %% @doc 'member' filter, test if an element is part of a list
  4. %% Copyright 2010 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(filter_member).
  18. -export([member/3]).
  19. member(_S, undefined, _Context) ->
  20. false;
  21. member(S, [H|_] = L, _Context) when is_list(S) andalso is_binary(H) ->
  22. lists:member(list_to_binary(S), L);
  23. member(S, [H|_] = L, _Context) when is_list(S) andalso is_integer(H) ->
  24. try
  25. lists:member(list_to_integer(S), L)
  26. catch
  27. _:_ -> false
  28. end;
  29. member(S, L, _Context) when is_list(L) ->
  30. lists:member(S, L);
  31. member(S, Value, Context) ->
  32. case erlydtl_runtime:to_list(Value, Context) of
  33. L when is_list(L) -> member(S, L, Context);
  34. _ -> undefined
  35. end.