Permalink
Browse files

Merge branch 'master' of git://github.com/philomat/django-cms-2.0

  • Loading branch information...
2 parents 4957230 + 78a41aa commit c0dc428023b139e5bcfaf815b3a02eb6682b372b @digi604 digi604 committed Mar 25, 2010
Showing with 29 additions and 6 deletions.
  1. +13 −2 cms/docs/templatetags.txt
  2. +16 −4 cms/templatetags/cms_tags.py
@@ -8,7 +8,7 @@ To use any of the following templatetags you need to load them first at the top
placeholder
-----------
-The placeholder templatetag will be filled with plugins. The cms auto-detects all placeholders
+The ``placeholder`` templatetag will be filled with plugins. The cms auto-detects all placeholders
used in the template.
Example::
@@ -23,7 +23,18 @@ If a width is provided every plugin recieves a "width" context variable when the
The main use case of the width attribute is for automatically resizing images correctly.
Also be sure to check out the ``PLACEHOLDER_CONF`` setting where you can change some of the behavior
-of the placeholders.
+of the placeholders.
+
+placeholderor
+-------------
+
+The same as ``placeholder``, with additional content to be displayed in case the placeholder is empty:
+If the placeholder has no plugins or the plugins do not generate any output, everything between
+``{% placeholderor %}`` and ``{% endplaceholderor %}`` is rendered instead.
+
+Example::
+
+ {% placeholderor "content" %}There is no content.{% endplaceholderor %}
show_placeholder_by_id
----------------------
@@ -103,14 +103,21 @@ def do_placeholder(parser, token):
try:
# split_contents() knows not to split quoted strings.
bits = token.split_contents()
+ # if the `placeholderor` tag was used, look for closing tag, and pass the enclosed nodes
+ # to PlaceholderNode below
+ if bits[0] == 'placeholderor':
+ nodelist_or = parser.parse(('endplaceholderor',))
+ parser.delete_first_token()
+ else:
+ nodelist_or = None
except ValueError:
raise template.TemplateSyntaxError(error_string)
if len(bits) == 2:
#tag_name, name
- return PlaceholderNode(bits[1])
+ return PlaceholderNode(bits[1], nodelist_or=nodelist_or)
elif len(bits) == 3:
#tag_name, name, width
- return PlaceholderNode(bits[1], bits[2])
+ return PlaceholderNode(bits[1], bits[2], nodelist_or=nodelist_or)
else:
raise template.TemplateSyntaxError(error_string)
@@ -124,9 +131,10 @@ class PlaceholderNode(template.Node):
name -- the name of the placeholder
width -- additional width attribute (integer) which gets added to the plugin context
"""
- def __init__(self, name, width=None):
+ def __init__(self, name, width=None, nodelist_or=None):
self.name = "".join(name.lower().split('"'))
if width: self.width = template.Variable(width)
+ self.nodelist_or = nodelist_or
def render(self, context):
width_var = getattr(self, 'width', None)
@@ -149,12 +157,16 @@ def render(self, context):
page = request.current_page
if page == "dummy":
return ""
- return render_plugins_for_context(self.name, page, context, width)
+ content = render_plugins_for_context(self.name, page, context, width)
+ if not content and self.nodelist_or:
+ return self.nodelist_or.render(context)
+ return content
def __repr__(self):
return "<Placeholder Node: %s>" % self.name
register.tag('placeholder', do_placeholder)
+register.tag('placeholderor', do_placeholder)
def do_page_attribute(parser, token):
error_string = '%r tag requires one argument' % token.contents[0]

0 comments on commit c0dc428

Please sign in to comment.