Login

WordWrap template tag

Author:
Daeg
Posted:
March 26, 2007
Language:
Python
Version:
.96
Tags:
template tags wordwrap
Score:
1 (after 1 ratings)

Basically a clone of the default "wrap" filter. I use it to generate plaintext e-mail that has to be broken at 75 characters.

{% wordwrap 80 %} Some text here, including other template tags, includes, etc. {% endwordwrap %}

I prefer this over the {% filter wordwrap:80 %} as my template (e-mail) writers keep screwing it up.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django import template
from django.utils.text import wrap

register = template.Library()

def wordwrap(parser, token):
	"""
	{% wordwrap 80 %}
	some really long text here, including other template functions, etc
	{% endwordwrap %}
	"""
	try:
		tag_name, len = token.split_contents()
	except ValueError:
		raise template.TemplateSyntaxError, "wordwrap tag requires exactly one argument"
	nodelist = parser.parse(('endwordwrap',))
	parser.delete_first_token()
	return WordWrapNode(nodelist, len)
wordwrap = register.tag(wordwrap)

class WordWrapNode(template.Node):
	def __init__(self, nodelist, len):
		self.nodelist = nodelist
		self.len = len
	
	def render(self, context):
		return wrap(str(self.nodelist.render(context)), int(self.len))

More like this

  1. E-mail quoting filters and tags by ChipX86 7 years, 9 months ago
  2. email_links by sansmojo 7 years, 10 months ago
  3. Template Tag to protect the E-mail address by nitinhayaran 5 years, 1 month ago
  4. Simple e-mail template tag by dchandek 6 years, 11 months ago
  5. HTML to text filter by MasonM 6 years, 7 months ago

Comments

Please login first before commenting.