1
from django.contrib.webdesign.lorem_ipsum import words, paragraphs
2
from django import template
4
register = template.Library()
6
class LoremNode(template.Node):
7
def __init__(self, count, method, common):
8
self.count, self.method, self.common = count, method, common
10
def render(self, context):
12
count = int(self.count.resolve(context))
13
except (ValueError, TypeError):
15
if self.method == 'w':
16
return words(count, common=self.common)
18
paras = paragraphs(count, common=self.common)
19
if self.method == 'p':
20
paras = ['<p>%s</p>' % p for p in paras]
21
return u'\n\n'.join(paras)
24
def lorem(parser, token):
26
Creates random Latin text useful for providing test data in templates.
30
{% lorem [count] [method] [random] %}
32
``count`` is a number (or variable) containing the number of paragraphs or
33
words to generate (default is 1).
35
``method`` is either ``w`` for words, ``p`` for HTML paragraphs, ``b`` for
36
plain-text paragraph blocks (default is ``b``).
38
``random`` is the word ``random``, which if given, does not use the common
39
paragraph (starting "Lorem ipsum dolor sit amet, consectetuer...").
42
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph
43
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph
44
and two random paragraphs each wrapped in HTML ``<p>`` tags
45
* ``{% lorem 2 w random %}`` will output two random latin words
47
bits = list(token.split_contents())
50
common = bits[-1] != 'random'
54
if bits[-1] in ('w', 'p', 'b'):
63
count = parser.compile_filter(count)
65
raise template.TemplateSyntaxError("Incorrect format for %r tag" % tagname)
66
return LoremNode(count, method, common)
67
lorem = register.tag(lorem)