Skip to content
This repository

Have truncatewords/2 ignore tuples (e.g. timestamp) and truncatewords/3 ... #40

Merged
merged 2 commits into from over 1 year ago

2 participants

Rune Juhl Jacobsen Evan Miller
Rune Juhl Jacobsen

...ignore atoms (e.g. null or undefined).

Evan Miller
Owner

Hmm. Why is this necessary? Why are tuples being passed to truncatewords in the first place?

Rune Juhl Jacobsen

Both tuples and atoms are passed in cb_admin. I figured that people would use truncatewords as you have done yourself, so it'd be better to have truncatewords not choke on them.
The error given was really quite unheplful, which had me baffled for a bit...

From cb_admin/src/view/model/show.html:

        {% for attr_name, attr_val in val.attributes %}
        {% ifequal "id" attr_name %}
        <td><strong><a href="{% url action="show" record_id=attr_val %}">{{ attr_val }}</a></strong></td>
        {% else %}
        {% if "_time" in attr_name %}
        <td id="{{ val.id }}-{{ attr_name }}">{{ attr_val|date:"N j, Y, P" }}</td>
        {% else %}
        <td id="{{ val.id }}-{{ attr_name }}">{{ attr_val|truncatewords:8 }}</td>
        {% endif %}
        {% endifequal %}
        {% endfor %}

Note that the above code handles time, but only when the column is named *_time. I'm using timestamp_*.

One could argue that atoms should be converted to lists and truncated, but my reasoning is that it doesn't make sense to truncate atoms. Consider truncated versions of 'this_is_a_really_important_atom' and 'this_is_totally_nonimportant' :)

Evan Miller
Owner
Rune Juhl Jacobsen

Ok. I'll update it accordingly. I'll have a look at cb_admin as well, when I have the time.

Evan Miller evanmiller merged commit 2f8504a into from August 20, 2012
Evan Miller evanmiller closed this August 20, 2012
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.

Showing 1 changed file with 2 additions and 0 deletions. Show diff stats Hide diff stats

  1. 2  src/erlydtl_filters.erl
2  src/erlydtl_filters.erl
@@ -1041,6 +1041,8 @@ truncatechars(_Input, 0, Acc) ->
1041 1041
 truncatechars([C|Rest], CharsLeft, Acc) ->
1042 1042
     truncatechars(Rest, CharsLeft - 1, [C|Acc]).
1043 1043
 
  1044
+truncatewords(Value, _WordsLeft, _Acc) when is_atom(Value) ->
  1045
+    Value;
1044 1046
 truncatewords([], _WordsLeft, Acc) ->
1045 1047
     lists:reverse(Acc);
1046 1048
 truncatewords(_Input, 0, Acc) ->
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.