Fixes #3799 -- Oops - ran last checkin from the wrong directory. Checked in the rest of the lorem templatetag implementation.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4848 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1f0b1afbfe
commit
5f68a26ff4
|
@ -280,6 +280,24 @@ class LoadNode(Node):
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
class LoremNode(Node):
|
||||||
|
def __init__(self, count, method, common):
|
||||||
|
self.count, self.method, self.common = count, method, common
|
||||||
|
|
||||||
|
def render(self, context):
|
||||||
|
from django.utils.lorem_ipsum import words, paragraphs
|
||||||
|
try:
|
||||||
|
count = int(self.count.resolve(context))
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
count = 1
|
||||||
|
if self.method == 'w':
|
||||||
|
return words(count, common=self.common)
|
||||||
|
else:
|
||||||
|
paras = paragraphs(count, common=self.common)
|
||||||
|
if self.method == 'p':
|
||||||
|
paras = ['<p>%s</p>' % p for p in paras]
|
||||||
|
return '\n\n'.join(paras)
|
||||||
|
|
||||||
class NowNode(Node):
|
class NowNode(Node):
|
||||||
def __init__(self, format_string):
|
def __init__(self, format_string):
|
||||||
self.format_string = format_string
|
self.format_string = format_string
|
||||||
|
@ -768,6 +786,52 @@ def load(parser, token):
|
||||||
return LoadNode()
|
return LoadNode()
|
||||||
load = register.tag(load)
|
load = register.tag(load)
|
||||||
|
|
||||||
|
#@register.tag
|
||||||
|
def lorem(parser, token):
|
||||||
|
"""
|
||||||
|
Creates random latin text useful for providing test data in templates.
|
||||||
|
|
||||||
|
Usage format::
|
||||||
|
|
||||||
|
{% lorem [count] [method] [random] %}
|
||||||
|
|
||||||
|
``count`` is a number (or variable) containing the number of paragraphs or
|
||||||
|
words to generate (default is 1).
|
||||||
|
|
||||||
|
``method`` is either ``w`` for words, ``p`` for HTML paragraphs, ``b`` for
|
||||||
|
plain-text paragraph blocks (default is ``b``).
|
||||||
|
|
||||||
|
``random`` is the word ``random``, which if given, does not use the common
|
||||||
|
paragraph (starting "Lorem ipsum dolor sit amet, consectetuer...").
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph
|
||||||
|
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph
|
||||||
|
and two random paragraphs each wrapped in HTML ``<p>`` tags
|
||||||
|
* ``{% lorem 2 w random %}`` will output two random latin words
|
||||||
|
"""
|
||||||
|
bits = list(token.split_contents())
|
||||||
|
tagname = bits[0]
|
||||||
|
# Random bit
|
||||||
|
common = bits[-1] != 'random'
|
||||||
|
if not common:
|
||||||
|
bits.pop()
|
||||||
|
# Method bit
|
||||||
|
if bits[-1] in ('w', 'p', 'b'):
|
||||||
|
method = bits.pop()
|
||||||
|
else:
|
||||||
|
method = 'b'
|
||||||
|
# Count bit
|
||||||
|
if len(bits) > 1:
|
||||||
|
count = bits.pop()
|
||||||
|
else:
|
||||||
|
count = '1'
|
||||||
|
count = parser.compile_filter(count)
|
||||||
|
if len(bits) != 1:
|
||||||
|
raise TemplateSyntaxError, "Incorrect format for %r tag" % tagname
|
||||||
|
return LoremNode(count, method, common)
|
||||||
|
lorem = register.tag(lorem)
|
||||||
|
|
||||||
#@register.tag
|
#@register.tag
|
||||||
def now(parser, token):
|
def now(parser, token):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -625,6 +625,31 @@ Load a custom template tag set.
|
||||||
|
|
||||||
See `Custom tag and filter libraries`_ for more information.
|
See `Custom tag and filter libraries`_ for more information.
|
||||||
|
|
||||||
|
lorem
|
||||||
|
~~~~~
|
||||||
|
|
||||||
|
Display random latin text useful for providing sample data in templates.
|
||||||
|
|
||||||
|
Usage format: ``{% lorem [count] [method] [random] %}``
|
||||||
|
|
||||||
|
=========== =============================================================
|
||||||
|
Argument Description
|
||||||
|
=========== =============================================================
|
||||||
|
``count`` A number (or variable) containing the number of paragraphs or
|
||||||
|
words to generate (default is 1).
|
||||||
|
``method`` Either ``w`` for words, ``p`` for HTML paragraphs or ``b``
|
||||||
|
for plain-text paragraph blocks (default is ``b``).
|
||||||
|
``random`` The word ``random``, which if given, does not use the common
|
||||||
|
paragraph ("Lorem ipsum dolor sit amet...") when generating
|
||||||
|
text.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph.
|
||||||
|
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph
|
||||||
|
and two random paragraphs each wrapped in HTML ``<p>`` tags.
|
||||||
|
* ``{% lorem 2 w random %}`` will output two random latin words.
|
||||||
|
|
||||||
now
|
now
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue