Django

Code

Ticket #5025 (reopened)

Opened 3 years ago

Last modified 3 weeks ago

Add a "truncate" template filter

Reported by: SmileyChris Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords:
Cc: semente@taurinus.org, paulschreiber@gmail.com, gabrielhurley, mikexstudios Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

A truncate filter seems to tie in nicely with the truncatewords and truncatewords_html filters (and it's something I needed recently).

The patch adds a truncate text util which is shared for the new truncate and the existing urlizetrunc filter.

Attachments

truncate.patch (4.3 kB) - added by SmileyChris on 07/31/07 00:43:23.

Change History

07/31/07 00:43:23 changed by SmileyChris

  • attachment truncate.patch added.

08/06/07 00:24:39 changed by adrian

  • needs_better_patch changed.
  • summary changed from truncate filter to Add a "truncate" template filter.
  • needs_tests changed.
  • needs_docs changed.

08/14/07 23:50:09 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

I'll push to DD since it's a complete patch, but inclusion needs approval.

12/01/07 14:49:33 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.
  • stage changed from Design decision needed to Accepted.

12/01/07 21:23:20 changed by SmileyChris

Huh? Fixed where?

12/01/07 21:23:24 changed by SmileyChris

  • status changed from closed to reopened.
  • resolution deleted.

06/02/09 12:19:10 changed by rjalves

This patch is more than two years old now, was this added at all?

06/03/09 00:22:28 changed by SmileyChris

06/03/09 00:28:04 changed by SmileyChris

  • stage changed from Accepted to Design decision needed.

But you're right, it has sat in Accepted for with a patch for long enough - it needs a design decision.

06/03/09 04:55:11 changed by rjalves

I tried slice, but it doesn't do exactly the same thing. The ... in the end give it a quite different meaning. At the moment I just implemented this filter as a custom filter on my application. I couldn't find any other built-in feature that would do the same task. I will be glad to drop the custom filter if this patch gets in though.

Thanks for the reply.

08/14/09 01:32:00 changed by EmilStenstrom

There are more differences between slice and truncate: slice doesn't work on things that are not strings, while truncate do. So trying to do {{ article|slice:":20" }} (hoping to limit the unicode_ representation of the article), will not work. {{ article|truncate:20 }} does work like intended.

The patch works fine and fills a real need. I'm voting +1 on adding this.

08/14/09 04:15:29 changed by SmileyChris

PS: Bringing it up on the django dev list makes more sense than adding a +1 here.

10/08/09 11:55:30 changed by Guilherme Gondim <semente@taurinus.org>

  • cc set to semente@taurinus.org.

I made a similar and simple filter, but your patch is better to Django.

For reference: http://www.djangosnippets.org/snippets/1715/.

11/11/09 22:46:55 changed by paulschreiber

The proposal in #12200 uses a unicode ellipsis and provides for head, tail and middle truncation. It also avoids breaking in the middle of words.

11/11/09 22:47:06 changed by paulschreiber

  • cc changed from semente@taurinus.org to semente@taurinus.org, paulschreiber@gmail.com.

03/06/10 15:30:19 changed by gabrielhurley

  • cc changed from semente@taurinus.org, paulschreiber@gmail.com to semente@taurinus.org, paulschreiber@gmail.com, gabrielhurley.

06/12/10 15:50:49 changed by mikexstudios

  • cc changed from semente@taurinus.org, paulschreiber@gmail.com, gabrielhurley to semente@taurinus.org, paulschreiber@gmail.com, gabrielhurley, mikexstudios.

07/09/10 06:01:24 changed by lukeplant

One problem with this filter is how to correctly handle unicode, where grapheme != code point. Perhaps some library can do it, I don't think that Python bundles such a library. I don't think this is as much of a problem with truncatewords because you rarely have a combining character preceding a space (is that ever valid?).


Add/Change #5025 (Add a "truncate" template filter)




Change Properties
Action