Commit 3bdf1a53 by Timothée Peignier

rename template tags to be more obvious

parent 30f3d637
......@@ -9,8 +9,8 @@ Describes how to use Pipeline when it is installed and configured.
Templatetags
============
Pipeline includes two template tags: ``compressed_css`` and ``compressed_js``,
in a template library called ``compressed``.
Pipeline includes two template tags: ``stylesheet`` and ``javascript``,
in a template library called ``pipeline``.
They are used to output the ``<link>`` and ``<script>``-tags for the
specified CSS/JavaScript-groups (as specified in the settings).
......@@ -35,10 +35,10 @@ Example
If you have specified the CSS-groups “colors” and “stats” and a JavaScript-group
with the name “scripts”, you would use the following code to output them all ::
{% load compressed %}
{% compressed_css 'colors' %}
{% compressed_css 'stats' %}
{% compressed_js 'scripts' %}
{% load pipeline %}
{% stylesheet 'colors' %}
{% stylesheet 'stats' %}
{% javascript 'scripts' %}
Collect static
==============
......
......@@ -7,11 +7,11 @@ from django.contrib.staticfiles.storage import staticfiles_storage
from pipeline.packager import PackageNotFound
from pipeline.utils import guess_type
from pipeline.templatetags.compressed import CompressedMixin
from pipeline.templatetags.pipeline import PipelineMixin
class PipelineExtension(CompressedMixin, Extension):
tags = set(['compressed_css', 'compressed_js'])
class PipelineExtension(PipelineMixin, Extension):
tags = set(['stylesheet', 'javascript'])
def parse(self, parser):
tag = next(parser.stream)
......@@ -21,10 +21,10 @@ class PipelineExtension(CompressedMixin, Extension):
raise TemplateSyntaxError("Bad package name", tag.lineno)
args = [package_name]
if tag.value == "compressed_css":
if tag.value == "stylesheet":
return nodes.CallBlock(self.call_method('package_css', args), [], [], []).set_lineno(tag.lineno)
if tag.value == "compressed_js":
if tag.value == "javascript":
return nodes.CallBlock(self.call_method('package_js', args), [], [], []).set_lineno(tag.lineno)
return []
......
......@@ -6,14 +6,14 @@ from django import template
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from pipeline.conf import settings
from pipeline.packager import Packager, PackageNotFound
from pipeline.utils import guess_type
from ..conf import settings
from ..packager import Packager, PackageNotFound
from ..utils import guess_type
register = template.Library()
class CompressedMixin(object):
class PipelineMixin(object):
def package_for(self, package_name, package_type):
package = {
'js': getattr(settings, 'PIPELINE_JS', {}).get(package_name, {}),
......@@ -42,7 +42,7 @@ class CompressedMixin(object):
return method(package, paths, templates=templates)
class CompressedCSSNode(CompressedMixin, template.Node):
class StylesheetNode(PipelineMixin, template.Node):
def __init__(self, name):
self.name = name
......@@ -68,7 +68,7 @@ class CompressedCSSNode(CompressedMixin, template.Node):
return '\n'.join(tags)
class CompressedJSNode(CompressedMixin, template.Node):
class JavascriptNode(PipelineMixin, template.Node):
def __init__(self, name):
self.name = name
......@@ -104,18 +104,18 @@ class CompressedJSNode(CompressedMixin, template.Node):
@register.tag
def compressed_css(parser, token):
def stylesheet(parser, token):
try:
tag_name, name = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE_CSS setting' % token.split_contents()[0])
return CompressedCSSNode(name)
return StylesheetNode(name)
@register.tag
def compressed_js(parser, token):
def javascript(parser, token):
try:
tag_name, name = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE_JS setting' % token.split_contents()[0])
return CompressedJSNode(name)
return JavascriptNode(name)
......@@ -17,36 +17,36 @@ class JinjaTest(TestCase):
PackageLoader('pipeline', 'templates'))
def test_no_package(self):
template = self.env.from_string(u"""{% compressed_css "unknow" %}""")
template = self.env.from_string(u"""{% stylesheet "unknow" %}""")
self.assertEqual(u'', template.render())
template = self.env.from_string(u"""{% compressed_js "unknow" %}""")
template = self.env.from_string(u"""{% javascript "unknow" %}""")
self.assertEqual(u'', template.render())
def test_package_css(self):
template = self.env.from_string(u"""{% compressed_css "screen" %}""")
template = self.env.from_string(u"""{% stylesheet "screen" %}""")
self.assertEqual(u'<link href="/static/screen.css" rel="stylesheet" type="text/css" />', template.render())
def test_package_css_disabled(self):
with pipeline_settings(PIPELINE_ENABLED=False):
template = self.env.from_string(u"""{% compressed_css "screen" %}""")
template = self.env.from_string(u"""{% stylesheet "screen" %}""")
self.assertEqual(u'''<link href="/static/pipeline/css/first.css" rel="stylesheet" type="text/css" />
<link href="/static/pipeline/css/second.css" rel="stylesheet" type="text/css" />
<link href="/static/pipeline/css/urls.css" rel="stylesheet" type="text/css" />''', template.render())
def test_package_js(self):
template = self.env.from_string(u"""{% compressed_js "scripts" %}""")
template = self.env.from_string(u"""{% javascript "scripts" %}""")
self.assertEqual(u'<script type="text/javascript" src="/static/scripts.js" charset="utf-8"></script>', template.render())
def test_package_js_async(self):
template = self.env.from_string(u"""{% compressed_js "scripts_async" %}""")
template = self.env.from_string(u"""{% javascript "scripts_async" %}""")
self.assertEqual(u'<script async type="text/javascript" src="/static/scripts_async.js" charset="utf-8"></script>', template.render())
def test_package_js_defer(self):
template = self.env.from_string(u"""{% compressed_js "scripts_defer" %}""")
template = self.env.from_string(u"""{% javascript "scripts_defer" %}""")
self.assertEqual(u'<script defer type="text/javascript" src="/static/scripts_defer.js" charset="utf-8"></script>', template.render())
def test_package_js_async_defer(self):
template = self.env.from_string(u"""{% compressed_js "scripts_async_defer" %}""")
template = self.env.from_string(u"""{% javascript "scripts_async_defer" %}""")
self.assertEqual(u'<script async defer type="text/javascript" src="/static/scripts_async_defer.js" charset="utf-8"></script>', template.render())
......@@ -55,25 +55,25 @@ class DjangoTest(TestCase):
return Template(template).render(Context())
def test_compressed_empty(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_css "unknow" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% stylesheet "unknow" %}""")
self.assertEqual(u"", rendered)
def test_compressed_css(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_css "screen" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% stylesheet "screen" %}""")
self.assertEqual(u'<link href="/static/screen.css" rel="stylesheet" type="text/css" />', rendered)
def test_compressed_js(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_js "scripts" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% javascript "scripts" %}""")
self.assertEqual(u'<script type="text/javascript" src="/static/scripts.js" charset="utf-8"></script>', rendered)
def test_compressed_js_async(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_js "scripts_async" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% javascript "scripts_async" %}""")
self.assertEqual(u'<script async type="text/javascript" src="/static/scripts_async.js" charset="utf-8"></script>', rendered)
def test_compressed_js_defer(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_js "scripts_defer" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% javascript "scripts_defer" %}""")
self.assertEqual(u'<script defer type="text/javascript" src="/static/scripts_defer.js" charset="utf-8"></script>', rendered)
def test_compressed_js_async_defer(self):
rendered = self.render_template(u"""{% load compressed %}{% compressed_js "scripts_async_defer" %}""")
rendered = self.render_template(u"""{% load pipeline %}{% javascript "scripts_async_defer" %}""")
self.assertEqual(u'<script async defer type="text/javascript" src="/static/scripts_async_defer.js" charset="utf-8"></script>', rendered)
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