Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added clauses to truncate null-values and integers correctly. #29

Closed
wants to merge 2 commits into from

3 participants

@gwitmond

You get these with the postgres-adapter from boss_db.

Error messages are:
{function_clause,
[{erlydtl_filters,truncatewords,
[1800,8,[]],
[{file,"src/erlydtl_filters.erl"},{line,994}]},
...

and
function_clause,
[{erlydtl_filters,truncatewords,
[null,8,[]],
[{file,"src/erlydtl_filters.erl"},{line,994}]},
...

@gwitmond gwitmond Added clauses to truncate null-values and integers correctly.
You get these with the postgres-adapter from boss_db.

Error messages are:
{function_clause,
    [{erlydtl_filters,truncatewords,
         [1800,8,[]],
         [{file,"src/erlydtl_filters.erl"},{line,994}]},
    ...

and
function_clause,
    [{erlydtl_filters,truncatewords,
         [null,8,[]],
         [{file,"src/erlydtl_filters.erl"},{line,994}]},
    ...
bb6c24e
@evanmiller
Owner

Looks OK but this should be more general, i.e. work with all atoms not just 'null'.

@gwitmond gwitmond Added filter date_unix_epoch that displays unix time in seconds since…
… 1-1-1970.

Added clauses to display 'undefined' for date and floatformat when input is undefined. This prevents an rendering error.
313edd5
@evanmiller
Owner

Is date_unix_epoch standard in Django?

@kaos
Owner

I don't find date_unix_epoch among the built-in filters of django.

However, I like the changes that avoids crash on invalid input. But as Evan was into, I'd prefer them to be more generic. How about a catch all clause like:
filter(Invalid) -> io_lib:format("~p", [Invalid]).

Either that, or, have the runtime catch errors when applying filters and log the error, and return the empty string to the rendered result (look at the logs for errors, avoid leaking errors to potential end user).

@kaos kaos was assigned
@kaos kaos closed this pull request from a commit
@kaos kaos Merge branch 'gwitmond-null-and-int'
Fixes #29.
c2001a9
@kaos kaos closed this in c2001a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 5, 2012
  1. @gwitmond

    Added clauses to truncate null-values and integers correctly.

    gwitmond authored
    You get these with the postgres-adapter from boss_db.
    
    Error messages are:
    {function_clause,
        [{erlydtl_filters,truncatewords,
             [1800,8,[]],
             [{file,"src/erlydtl_filters.erl"},{line,994}]},
        ...
    
    and
    function_clause,
        [{erlydtl_filters,truncatewords,
             [null,8,[]],
             [{file,"src/erlydtl_filters.erl"},{line,994}]},
        ...
Commits on Jul 19, 2012
  1. @gwitmond

    Added filter date_unix_epoch that displays unix time in seconds since…

    gwitmond authored
    … 1-1-1970.
    
    Added clauses to display 'undefined' for date and floatformat when input is undefined. This prevents an rendering error.
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 0 deletions.
  1. +24 −0 src/erlydtl_filters.erl
View
24 src/erlydtl_filters.erl
@@ -53,6 +53,7 @@
cut/2,
date/1,
date/2,
+ date_unix_epoch/1,
default/2,
default_if_none/2,
dictsort/2,
@@ -195,8 +196,12 @@ date(Input) ->
date(Input, "F j, Y").
%% @doc Formats a date according to the given format.
+date(undefined, _) -> "undefined";
date(Input, FormatStr) when is_binary(Input) ->
list_to_binary(date(binary_to_list(Input), FormatStr));
+%% @doc format calendar:gregorian_seconds to string (year starts at year 0, not 1970. Use date_unix_epoch for latter).
+date(Input, FormatStr) when is_integer(Input) ->
+ date(calendar:gregorian_seconds_to_datetime(Input), FormatStr);
date({{_,_,_} = Date,{_,_,_} = Time}, FormatStr) ->
erlydtl_dateformat:format({Date, Time}, FormatStr);
date({_,_,_} = Date, FormatStr) ->
@@ -204,6 +209,12 @@ date({_,_,_} = Date, FormatStr) ->
date(Input, _FormatStr) when is_list(Input) ->
io:format("Unexpected date parameter : ~p~n", [Input]),
"".
+%% @doc format a date given in unix-epoch seconds.
+%% Input is seconds since 1-1-1970.
+%%% offset is calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}), the difference between
+%%% gregorian calendar and unix epoch.
+date_unix_epoch(Input) ->
+ date(Input + 62167219200).
%% @doc If value evaluates to `false', use given default. Otherwise, use the value.
default(Input, Default) ->
@@ -275,8 +286,12 @@ fix_ampersands(Input) when is_list(Input) ->
%% @doc When used without an argument, rounds a floating-point number to one decimal place
%% -- but only if there's a decimal part to be displayed
+floatformat(undefined, _) ->
+ "undefined";
floatformat(Number, Place) when is_binary(Number) ->
floatformat(binary_to_list(Number), Place);
+floatformat(Number, Place) when is_list(Number) ->
+ floatformat(list_to_float(Number), Place);
floatformat(Number, Place) ->
floatformat_io(Number, cast_to_integer(Place)).
@@ -445,6 +460,7 @@ phone2numeric(Input) when is_list(Input) ->
%% @doc Returns a plural suffix if the value is not 1. By default, this suffix is 's'.
pluralize(Number, Suffix) when is_binary(Suffix) ->
pluralize_io(Number, binary_to_list(Suffix) );
+
pluralize(Number, Suffix) when is_list(Suffix) ->
pluralize_io(Number, Suffix).
@@ -752,6 +768,10 @@ title(Input) when is_list(Input) ->
%% @doc Truncates a string after a certain number of characters.
truncatechars(_Input, Max) when Max =< 0 ->
"";
+truncatechars(null, Max) ->
+ truncatechars("null", Max);
+truncatechars(Input, Max) when is_integer(Input) ->
+ truncatechars(integer_to_list(Input), Max);
truncatechars(Input, Max) when is_binary(Input) ->
list_to_binary(truncatechars(binary_to_list(Input), Max));
truncatechars(Input, Max) ->
@@ -760,6 +780,10 @@ truncatechars(Input, Max) ->
%% @doc Truncates a string after a certain number of words.
truncatewords(_Input, Max) when Max =< 0 ->
"";
+truncatewords(null, Max) ->
+ truncatewords("null", Max);
+truncatewords(Input, Max) when is_integer(Input) ->
+ truncatewords(integer_to_list(Input), Max);
truncatewords(Input, Max) when is_binary(Input) ->
list_to_binary(truncatewords(binary_to_list(Input), Max));
truncatewords(Input, Max) ->
Something went wrong with that request. Please try again.