Login

Line & paragraph chopping

Author:
ludvig.ericson
Posted:
February 27, 2007
Language:
Python
Version:
Pre .96
Score:
2 (after 2 ratings)

I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic")

Use like so:

{% load myExtraModule %}
{{ blogpost.content|paragraphs:"2" }}

The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.template import Library

register = Library()

@register.filter
def paragraphs(var, arg):
    paras = var.replace("\r\n", "\n").split("\n\n")
    return "\n\n".join(paras[:int(arg)])

@register.filter
def lines(var, arg):
    lines = var.replace("\r\n", "\n").split("\n")
    return "\n".join(lines[:int(arg)])

More like this

  1. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 2 months, 3 weeks ago
  2. Python Django CRUD Example Tutorial by tuts_station 3 months ago
  3. Browser-native date input field by kytta 4 months, 2 weeks ago
  4. Generate and render HTML Table by LLyaudet 4 months, 4 weeks ago
  5. My firs Snippets by GutemaG 5 months ago

Comments

Please login first before commenting.