Commit b647cf55 by Christian Hammond

Split the render_compressed method in templatetags for easier subclassing.

The base mixin for the template tags, PipelineMixin, performed the bulk
of its rendering logic in the render_compressed() method. It handled
both rendering output files (if PIPELINE_ENABLED was True), or source
files (if False). Due to the complexity of this method, it was difficult
for a subclass (which may need to perform more specialized logic) to
override this method without copying the code.

This splits out that function into two new utility methods,
render_compressed_sources() and render_compressed_output(). These take
the same arguments as render_compressed(), and are called by that method
as well. This allows a subclass to more easily replace the logic for
determining which to call, or to replace the logic for either of these
renders.
parent c37c9f73
...@@ -54,29 +54,66 @@ class PipelineMixin(object): ...@@ -54,29 +54,66 @@ class PipelineMixin(object):
pass pass
def render_compressed(self, package, package_name, package_type): def render_compressed(self, package, package_name, package_type):
"""Render HTML for the package.
If ``PIPELINE_ENABLED`` is ``True``, this will render the package's
output file (using :py:meth:`render_compressed_output`). Otherwise,
this will render the package's source files (using
:py:meth:`render_compressed_sources`).
Subclasses can override this method to provide custom behavior for
determining what to render.
"""
if settings.PIPELINE_ENABLED: if settings.PIPELINE_ENABLED:
method = getattr(self, "render_{0}".format(package_type)) return self.render_compressed_output(package, package_name,
return method(package, package.output_filename) package_type)
else: else:
if settings.PIPELINE_COLLECTOR_ENABLED: return self.render_compressed_sources(package, package_name,
default_collector.collect(self.request) package_type)
def render_compressed_output(self, package, package_name, package_type):
"""Render HTML for using the package's output file.
Subclasses can override this method to provide custom behavior for
rendering the output file.
"""
method = getattr(self, 'render_{0}'.format(package_type))
return method(package, package.output_filename)
def render_compressed_sources(self, package, package_name, package_type):
"""Render HTML for using the package's list of source files.
packager = Packager() Each source file will first be collected, if
method = getattr(self, "render_individual_{0}".format(package_type)) ``PIPELINE_COLLECTOR_ENABLED`` is ``True``.
If there are any errors compiling any of the source files, an
``SHOW_ERRORS_INLINE`` is ``True``, those errors will be shown at
the top of the page.
Subclasses can override this method to provide custom behavior for
rendering the source files.
"""
if settings.PIPELINE_COLLECTOR_ENABLED:
default_collector.collect(self.request)
packager = Packager()
method = getattr(self, 'render_individual_{0}'.format(package_type))
try:
paths = packager.compile(package.paths)
except CompilerError as e:
if settings.SHOW_ERRORS_INLINE:
method = getattr(self, 'render_error_{0}'.format(
package_type))
try: return method(package_name, e)
paths = packager.compile(package.paths) else:
except CompilerError as e: raise
if settings.SHOW_ERRORS_INLINE:
method = getattr(self, 'render_error_{0}'.format(
package_type))
return method(package_name, e) templates = packager.pack_templates(package)
else:
raise
templates = packager.pack_templates(package) return method(package, paths, templates=templates)
return method(package, paths, templates=templates)
def render_error(self, package_type, package_name, e): def render_error(self, package_type, package_name, e):
return render_to_string('pipeline/compile_error.html', Context({ return render_to_string('pipeline/compile_error.html', Context({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment