Introduced a new 'compressed' context dictionary to be passed to the templates to prevent accidental overriding of context variables with common names (e.g. "name", "url", "content"). Fixes #150, #151, #156.

This commit is contained in:
Jannis Leidel
2011-11-24 13:54:17 +01:00
parent 218c24b4e0
commit bba941c341
9 changed files with 50 additions and 17 deletions

View File

@@ -258,13 +258,14 @@ class Compressor(object):
Renders the compressor output with the appropriate template for
the given mode and template context.
"""
if context is None:
context = {}
final_context = Context()
final_context.update(self.context)
final_context.update(context)
final_context.update(self.extra_context)
post_compress.send(sender='django-compressor', type=self.type,
# Just in case someone renders the compressor outside
# the usual template rendering cycle
if 'compressed' not in self.context:
self.context['compressed'] = {}
self.context['compressed'].update(context or {})
self.context['compressed'].update(self.extra_context)
final_context = Context(self.context)
mode=mode, context=final_context)
return render_to_string("compressor/%s_%s.html" %
(self.type, mode), final_context)

View File

@@ -1 +1 @@
<link rel="stylesheet" href="{{ url }}" type="text/css"{% if media %} media="{{ media }}"{% endif %} />
<link rel="stylesheet" href="{{ compressed.url }}" type="text/css"{% if compressed.media %} media="{{ compressed.media }}"{% endif %} />

View File

@@ -1 +1 @@
<style type="text/css"{% if media %} media="{{ media }}"{% endif %}>{{ content|safe }}</style>
<style type="text/css"{% if compressed.media %} media="{{ compressed.media }}"{% endif %}>{{ compressed.content|safe }}</style>

View File

@@ -1 +1 @@
<script type="text/javascript" src="{{ url }}"></script>
<script type="text/javascript" src="{{ compressed.url }}"></script>

View File

@@ -1 +1 @@
<script type="text/javascript">{{ content|safe }}</script>
<script type="text/javascript">{{ compressed.content|safe }}</script>

View File

@@ -81,10 +81,13 @@ class CompressorNode(template.Node):
return cached_offline
# Prepare the compressor
context.update({'name': self.name})
if 'compressed' in context:
raise template.TemplateSyntaxError("A context variable named "
"'compresse' was found in the context. Make sure to name "
"it differently as this name is reserved to Compressor.")
context['compressed'] = {'name': self.name}
compressor = self.compressor_cls(content=self.nodelist.render(context),
context=context)
# Check cache
cache_key, cache_content = self.render_cached(compressor, forced)
if cache_content is not None:

View File

@@ -1,9 +1,38 @@
Changelog
=========
v1.1.2
v1.2.0
------
- Introduced a new ``compressed`` context dictionary that is passed to
the templates that are responsible for rendering the compressed snippets.
This is a **backwards-incompatible change** if you've overridden any of
the included templates:
- ``compressor/css_file.html``
- ``compressor/css_inline.html``
- ``compressor/js_file.html``
- ``compressor/js_inline.html``
The variables passed to those templates have been namespaced in a
dictionary, so it's easy to fix your own templates.
For example, the old ``compressor/js_file.html``::
<script type="text/javascript" src="{{ url }}"></script>
The new ``compressor/js_file.html``::
<script type="text/javascript" src="{{ compressed.url }}"></script>
- Removed old templates named ``compressor/css.html`` and
``compressor/js.html`` that were originally left for backwards
compatibility. If you've overridden them, just rename them to
``compressor/css_file.html`` or ``compressor/js_file.html`` and
make sure you've accounted for the backwards incompatible change
of the template context mentioned above.
- Reverted an unfortunate change to the YUI filter that prepended
``'java -jar'`` to the binary name, which doesn't alway work, e.g.
if the YUI compressor is shipped as a script like

View File

@@ -38,7 +38,7 @@ class PostCompressSignalTestCase(TestCase):
self.assertEquals('js', kwargs['type'])
self.assertEquals('file', kwargs['mode'])
context = kwargs['context']
assert 'url' in context
assert 'url' in context['compressed']
def test_css_signal_sent(self):
def listener(sender, **kwargs):
@@ -51,7 +51,7 @@ class PostCompressSignalTestCase(TestCase):
self.assertEquals('css', kwargs['type'])
self.assertEquals('file', kwargs['mode'])
context = kwargs['context']
assert 'url' in context
assert 'url' in context['compressed']
def test_css_signal_multiple_media_attributes(self):
css = """\

View File

@@ -121,4 +121,4 @@ class TemplatetagTestCase(TestCase):
render(template)
args, kwargs = callback.call_args
context = kwargs['context']
self.assertEqual('foo', context['name'])
self.assertEqual('foo', context['compressed']['name'])