0

In my django templates i have a list of objects which is rendered as follows:

<li class="keys">
{% for key in job.key_list|slice:":2" %}
    {% if not forloop.last %}
        <a href="/keys/{{ key.id }}/{{ key }}/">{{ key }}</a>,
    {% else %}
        <a href="/keys/{{ key.id }}/{{ key }}/">{{ key }}</a>
    {% endif %}
{% endfor %}
</li>

This outputs the list as:

some_key, some_key_two

I want to truncate the number of characters to 20 but on the whole list. So that it renders something like:

some_key, some_key_t...

I am aware about the truncatechars and truncatewords filters available in django but they work on a string variable within the template.

How do i go about implementing this functionality ?

Amyth
  • 32,527
  • 26
  • 93
  • 135
  • It's not really clear what you're after. Do you want to truncate the `key` in the href part of the `` or the `key` in the text of the `` ? – Henrik Andersson May 20 '13 at 09:04
  • i do not want to truncate a single object, For example: if i use `truncatechars` to 6 chars on elements say (Stack Overflow and Stack Exchange) it will output. `Stack ..., Stack ...`. I want to truncate the characters in the rendered list so that if i truncate on 20 characters it looks like `Stack Overflow, Stac...`. So here the total number of characters shown within the `
  • ` element is 20.
  • – Amyth May 20 '13 at 09:12
  • Don't think django can help you here. Try to do that through CSS by specifying `max-width` and `overflow: hidden`. Or format the strings in view and pass them in list to template. – Rohan May 20 '13 at 09:21
  • @Rohan: Yeah I thought so, maybe an easier solution would be to use a jquery plugin like expander. – Amyth May 20 '13 at 09:33