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