Skip to content

Commit 0dc5858

Browse files
committed
Merge pull request jazzband#552 from chipx86/templatetags/split-render-func
Split render_compressed in templatetags for easier subclassing
2 parents c37c9f7 + b647cf5 commit 0dc5858

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

pipeline/templatetags/pipeline.py

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,66 @@ def render(self, context):
5454
pass
5555

5656
def render_compressed(self, package, package_name, package_type):
57+
"""Render HTML for the package.
58+
59+
If ``PIPELINE_ENABLED`` is ``True``, this will render the package's
60+
output file (using :py:meth:`render_compressed_output`). Otherwise,
61+
this will render the package's source files (using
62+
:py:meth:`render_compressed_sources`).
63+
64+
Subclasses can override this method to provide custom behavior for
65+
determining what to render.
66+
"""
5767
if settings.PIPELINE_ENABLED:
58-
method = getattr(self, "render_{0}".format(package_type))
59-
return method(package, package.output_filename)
68+
return self.render_compressed_output(package, package_name,
69+
package_type)
6070
else:
61-
if settings.PIPELINE_COLLECTOR_ENABLED:
62-
default_collector.collect(self.request)
71+
return self.render_compressed_sources(package, package_name,
72+
package_type)
73+
74+
def render_compressed_output(self, package, package_name, package_type):
75+
"""Render HTML for using the package's output file.
76+
77+
Subclasses can override this method to provide custom behavior for
78+
rendering the output file.
79+
"""
80+
method = getattr(self, 'render_{0}'.format(package_type))
81+
82+
return method(package, package.output_filename)
83+
84+
def render_compressed_sources(self, package, package_name, package_type):
85+
"""Render HTML for using the package's list of source files.
6386
64-
packager = Packager()
65-
method = getattr(self, "render_individual_{0}".format(package_type))
87+
Each source file will first be collected, if
88+
``PIPELINE_COLLECTOR_ENABLED`` is ``True``.
89+
90+
If there are any errors compiling any of the source files, an
91+
``SHOW_ERRORS_INLINE`` is ``True``, those errors will be shown at
92+
the top of the page.
93+
94+
Subclasses can override this method to provide custom behavior for
95+
rendering the source files.
96+
"""
97+
if settings.PIPELINE_COLLECTOR_ENABLED:
98+
default_collector.collect(self.request)
99+
100+
packager = Packager()
101+
method = getattr(self, 'render_individual_{0}'.format(package_type))
102+
103+
try:
104+
paths = packager.compile(package.paths)
105+
except CompilerError as e:
106+
if settings.SHOW_ERRORS_INLINE:
107+
method = getattr(self, 'render_error_{0}'.format(
108+
package_type))
66109

67-
try:
68-
paths = packager.compile(package.paths)
69-
except CompilerError as e:
70-
if settings.SHOW_ERRORS_INLINE:
71-
method = getattr(self, 'render_error_{0}'.format(
72-
package_type))
110+
return method(package_name, e)
111+
else:
112+
raise
73113

74-
return method(package_name, e)
75-
else:
76-
raise
114+
templates = packager.pack_templates(package)
77115

78-
templates = packager.pack_templates(package)
79-
return method(package, paths, templates=templates)
116+
return method(package, paths, templates=templates)
80117

81118
def render_error(self, package_type, package_name, e):
82119
return render_to_string('pipeline/compile_error.html', Context({

0 commit comments

Comments
 (0)