from django import template register = template.Library() @register.filter("inline_truncate") def inline_truncate(value, size): """Truncates a string to the given size placing the ellipsis at the middle of the string""" if len(value) > size and size > 3: start = (size - 3) / 2 end = (size - 3) - start return value[0:start] + '...' + value[-end:] else: return value[0:size]