Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

truncatechars template filter from django dev. #20

Merged
merged 1 commit into from

2 participants

@nyaray

I thought I needed the truncatechars filter for a task, figured out that it didn't exist and implemented it. Then changed my mind and decided not to use it, but it feels like a waste of time not to share it :).

@evanmiller evanmiller merged commit 3330835 into from
@evanmiller
Owner

Looks good, thanks!

@nyaray

Whoa, that was quick :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  README.markdown
@@ -7,7 +7,7 @@ ErlyDTL compiles Django Template Language to Erlang bytecode.
_Unsupported tags_: csrf_token, ifchanged, regroup, url
-*Supported filters*: add, addslashes, capfirst, center, cut, date, default, default_if_none, dictsort, dictsortreversed, divisibleby, escape, escapejs, filesizeformat, first, fix_ampersands, floatformat, force_escape, format_integer, format_number, get_digit, iriencode, join, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phonenumeric, pluralize, pprint, random, random_num, random_range, removetags, rjust, safe, safeseq, slice, slugify, stringformat, striptags, time, timesince, timeuntil, title, truncatewords, truncatewords_html, unordered_list, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, yesno
+*Supported filters*: add, addslashes, capfirst, center, cut, date, default, default_if_none, dictsort, dictsortreversed, divisibleby, escape, escapejs, filesizeformat, first, fix_ampersands, floatformat, force_escape, format_integer, format_number, get_digit, iriencode, join, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phonenumeric, pluralize, pprint, random, random_num, random_range, removetags, rjust, safe, safeseq, slice, slugify, stringformat, striptags, time, timesince, timeuntil, title, truncatechars, truncatewords, truncatewords_html, unordered_list, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, yesno
_Unsupported filters_: _none_
View
16 src/erlydtl_filters.erl
@@ -101,6 +101,7 @@
timeuntil/1,
timeuntil/2,
title/1,
+ truncatechars/2,
truncatewords/2,
truncatewords_html/2,
unordered_list/1,
@@ -748,6 +749,14 @@ title(Input) when is_binary(Input) ->
title(Input) when is_list(Input) ->
title(Input, []).
+%% @doc Truncates a string after a certain number of characters.
+truncatechars(_Input, Max) when Max =< 0 ->
+ "";
+truncatechars(Input, Max) when is_binary(Input) ->
+ list_to_binary(truncatechars(binary_to_list(Input), Max));
+truncatechars(Input, Max) ->
+ truncatechars(Input, Max, []).
+
%% @doc Truncates a string after a certain number of words.
truncatewords(_Input, Max) when Max =< 0 ->
"";
@@ -975,6 +984,13 @@ title([Char | Rest], [$\ |_] = Acc) when Char >= $a, Char =< $z ->
title([Char | Rest], Acc) ->
title(Rest, [Char | Acc]).
+truncatechars([], _CharsLeft, Acc) ->
+ lists:reverse(Acc);
+truncatechars(_Input, 0, Acc) ->
+ lists:reverse("..." ++ Acc);
+truncatechars([C|Rest], CharsLeft, Acc) ->
+ truncatechars(Rest, CharsLeft - 1, [C|Acc]).
+
truncatewords([], _WordsLeft, Acc) ->
lists:reverse(Acc);
truncatewords(_Input, 0, Acc) ->
View
9 tests/src/erlydtl_unittests.erl
@@ -766,6 +766,15 @@ tests() ->
{"|title (pre-formatted)",
<<"{{ \"My Title Case\"|title }}">>, [],
<<"My Title Case">>},
+ {"|truncatechars:0",
+ <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
+ <<"">>},
+ {"|truncatechars:11",
+ <<"{{ var1|truncatechars:11 }}">>, [{var1, "Truncate Me Please"}],
+ <<"Truncate Me...">>},
+ {"|truncatechars:17",
+ <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
+ <<"Don't Truncate Me">>},
{"|truncatewords:0",
<<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
<<"">>},
Something went wrong with that request. Please try again.