Skip to content

Commit

Permalink
Merge pull request #3768 from yakky/feature/render_placeholder_uncached
Browse files Browse the repository at this point in the history
Add render_uncached_placeholder templatetag
  • Loading branch information
yakky committed Feb 21, 2015
2 parents 3f3a963 + 2eaf0bd commit 0b0b4bb
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 20 deletions.
14 changes: 14 additions & 0 deletions cms/templatetags/cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,20 @@ def get_value(self, context, **kwargs):
register.tag(RenderPlaceholder)


class RenderUncachedPlaceholder(RenderPlaceholder):
"""
Uncached version of RenderPlaceholder
This templatetag will neither get the result from cache, nor will update
the cache value for the given placeholder
"""
name = 'render_uncached_placeholder'

def _get_value(self, context, editable=True, **kwargs):
kwargs['nocache'] = True
return super(RenderUncachedPlaceholder, self)._get_value(context, editable, **kwargs)

register.tag(RenderUncachedPlaceholder)

NULL = object()


Expand Down
78 changes: 78 additions & 0 deletions cms/tests/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,84 @@ def test_render_placeholder_tag(self):
r
)

def test_render_uncached_placeholder_tag(self):
"""
Tests the {% render_uncached_placeholder %} templatetag.
"""
render_uncached_placeholder_body = "I'm the render uncached placeholder body"
ex1 = Example1(char_1="char_1", char_2="char_2", char_3="char_3",
char_4="char_4")
ex1.save()

add_plugin(ex1.placeholder, u"TextPlugin", u"en", body=render_uncached_placeholder_body)

t = '''{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
<h1>{% render_uncached_placeholder ex1.placeholder %}</h1>
<h2>{% render_uncached_placeholder ex1.placeholder as tempvar %}</h2>
<h3>{{ tempvar }}</h3>
{% endblock content %}
'''
r = self.render(t, self.test_page, {'ex1': ex1})
self.assertIn(
'<h1>%s</h1>' % render_uncached_placeholder_body,
r
)
self.assertIn(
'<h2></h2>',
r
)

self.assertIn(
'<h3>%s</h3>' % render_uncached_placeholder_body,
r
)

def test_render_uncached_placeholder_tag_no_use_cache(self):
"""
Tests that {% render_uncached_placeholder %} does not populate cache.
"""
render_uncached_placeholder_body = "I'm the render uncached placeholder body"
ex1 = Example1(char_1="char_1", char_2="char_2", char_3="char_3",
char_4="char_4")
ex1.save()

add_plugin(ex1.placeholder, u"TextPlugin", u"en", body=render_uncached_placeholder_body)

template = '{% load cms_tags %}<h1>{% render_uncached_placeholder ex1.placeholder %}</h1>'

cache_key = ex1.placeholder.get_cache_key(u"en")
cache_value_before = cache.get(cache_key)
self.render(template, self.test_page, {'ex1': ex1})
cache_value_after = cache.get(cache_key)

self.assertEqual(cache_value_before, cache_value_after)
self.assertIsNone(cache_value_after)

def test_render_placeholder_tag_use_cache(self):
"""
Tests that {% render_placeholder %} populates cache.
"""
render_placeholder_body = "I'm the render placeholder body"
ex1 = Example1(char_1="char_1", char_2="char_2", char_3="char_3",
char_4="char_4")
ex1.save()

add_plugin(ex1.placeholder, u"TextPlugin", u"en", body=render_placeholder_body)

template = '{% load cms_tags %}<h1>{% render_placeholder ex1.placeholder %}</h1>'

cache_key = ex1.placeholder.get_cache_key(u"en")
cache_value_before = cache.get(cache_key)
self.render(template, self.test_page, {'ex1': ex1})
cache_value_after = cache.get(cache_key)

self.assertNotEqual(cache_value_before, cache_value_after)
self.assertIsNone(cache_value_before)
self.assertIsNotNone(cache_value_after)

def test_show_placeholder(self):
"""
Tests the {% show_placeholder %} templatetag, using lookup by pk/dict/reverse_id and passing a Page object.
Expand Down
70 changes: 50 additions & 20 deletions docs/reference/templatetags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Example::
{% static_placeholder "footer" site or %}There is no content.{% endstatic_placeholder %}


.. templatetag:: show_placeholder
.. templatetag:: render_placeholder

render_placeholder
==================
Expand All @@ -124,6 +124,10 @@ The :ttag:`render_placeholder` tag takes the following parameters:
* ``width`` parameter for context sensitive plugins (optional)
* ``language`` keyword plus ``language-code`` string to render content in the
specified language (optional)
* ``language`` keyword plus ``language-code`` string to render content in the
specified language (optional)
* ``as`` keyword followed by ``varname`` (optional): the templatetag output can
be saved as a context variable for later use.


The following example renders the my_placeholder field from the mymodel_instance and will render
Expand All @@ -147,7 +151,29 @@ only the english plugins:
When used in this manner, the placeholder will not be displayed for
editing when the CMS is in edit mode.

.. templatetag:: render_uncached_placeholder

render_uncached_placeholder
===========================

The same as :ttag:`render_placeholder`, but the placeholder contents will not be
cached or taken from the cache.

Arguments:

* :class:`~cms.models.fields.PlaceholderField` instance
* ``width`` parameter for context sensitive plugins (optional)
* ``language`` keyword plus ``language-code`` string to render content in the
specified language (optional)
* ``as`` keyword followed by ``varname`` (optional): the templatetag output can
be saved as a context variable for later use.

Example::

{% render_uncached_placeholder mymodel_instance.my_placeholder language 'en' %}


.. templatetag:: show_placeholder

show_placeholder
================
Expand All @@ -169,6 +195,29 @@ Examples::
{% show_placeholder "content" request.current_page.parent_id %}
{% show_placeholder "teaser" request.current_page.get_root %}


.. templatetag:: show_uncached_placeholder

show_uncached_placeholder
=========================

The same as :ttag:`show_placeholder`, but the placeholder contents will not be
cached or taken from the cache.

Arguments:

- ``placeholder_name``
- ``page_lookup`` (see `page_lookup`_ for more information)
- ``language`` (optional)
- ``site`` (optional)

Example::

{% show_uncached_placeholder "footer" "footer_container_page" %}


.. templatetag:: page_lookup

page_lookup
===========

Expand Down Expand Up @@ -209,25 +258,6 @@ inherit the content of its root-level ancestor::
{% endplaceholder %}


.. templatetag:: show_uncached_placeholder

show_uncached_placeholder
=========================

The same as :ttag:`show_placeholder`, but the placeholder contents will not be
cached.

Arguments:

- ``placeholder_name``
- ``page_lookup`` (see `page_lookup`_ for more information)
- ``language`` (optional)
- ``site`` (optional)

Example::

{% show_uncached_placeholder "footer" "footer_container_page" %}

.. templatetag:: page_url


Expand Down

0 comments on commit 0b0b4bb

Please sign in to comment.