WordWrap template tag

 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. HTML to text filter by MasonM 4 years, 9 months ago
  2. email_links by sansmojo 5 years, 11 months ago
  3. Truncate filter by zalun 4 years ago
  4. E-mail quoting filters and tags by ChipX86 5 years, 11 months ago
  5. Simple e-mail template tag by dchandek 5 years, 1 month ago

Comments

(Forgotten your password?)