Add support for static tags / staticfiles app.
This commit is contained in:
parent
d75943cb42
commit
6753913790
|
@ -0,0 +1,38 @@
|
|||
from coffin import template
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
from coffin.templatetags.static import StaticExtension
|
||||
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
class StaticExtension(StaticExtension):
|
||||
"""Implements the {% static %} tag as provided by the ``staticfiles``
|
||||
contrib module.
|
||||
|
||||
Rreturns the URL to a file using staticfiles' storage backend.
|
||||
|
||||
Usage::
|
||||
|
||||
{% static path [as varname] %}
|
||||
|
||||
Examples::
|
||||
|
||||
{% static "myapp/css/base.css" %}
|
||||
{% static variable_with_path %}
|
||||
{% static "myapp/css/base.css" as admin_base_css %}
|
||||
{% static variable_with_path as varname %}
|
||||
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_statc_url(cls, path):
|
||||
return super(StaticExtension, cls).get_statc_url(
|
||||
staticfiles_storage.url(path))
|
||||
|
||||
|
||||
register.tag(StaticExtension)
|
||||
|
||||
|
||||
def static(path):
|
||||
return StaticExtension.get_static_url(path)
|
|
@ -107,4 +107,5 @@ def add_to_builtins(module_name):
|
|||
|
||||
add_to_builtins('coffin.template.defaulttags')
|
||||
add_to_builtins('coffin.template.defaultfilters')
|
||||
add_to_builtins('coffin.templatetags.static')
|
||||
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
try:
|
||||
from urllib.parse import urljoin
|
||||
except ImportError: # Python 2
|
||||
from urlparse import urljoin
|
||||
|
||||
from coffin.template import Library
|
||||
from jinja2.ext import Extension
|
||||
from jinja2 import nodes
|
||||
from django.utils.encoding import iri_to_uri
|
||||
|
||||
|
||||
register = Library()
|
||||
|
||||
|
||||
class PrefixExtension(Extension):
|
||||
|
||||
def parse(self, parser):
|
||||
stream = parser.stream
|
||||
lineno = stream.next().lineno
|
||||
|
||||
call_node = self.call_method('render')
|
||||
|
||||
if stream.next_if('name:as'):
|
||||
var = nodes.Name(stream.expect('name').value, 'store')
|
||||
return nodes.Assign(var, call_node).set_lineno(lineno)
|
||||
else:
|
||||
return nodes.Output([call_node]).set_lineno(lineno)
|
||||
|
||||
def render(self, name):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def get_uri_setting(cls, name):
|
||||
try:
|
||||
from django.conf import settings
|
||||
except ImportError:
|
||||
prefix = ''
|
||||
else:
|
||||
prefix = iri_to_uri(getattr(settings, name, ''))
|
||||
return prefix
|
||||
|
||||
|
||||
class GetStaticPrefixExtension(PrefixExtension):
|
||||
"""
|
||||
Populates a template variable with the static prefix,
|
||||
``settings.STATIC_URL``.
|
||||
|
||||
Usage::
|
||||
|
||||
{% get_static_prefix [as varname] %}
|
||||
|
||||
Examples::
|
||||
|
||||
{% get_static_prefix %}
|
||||
{% get_static_prefix as static_prefix %}
|
||||
|
||||
"""
|
||||
|
||||
tags = set(['get_static_prefix'])
|
||||
|
||||
def render(self):
|
||||
return self.get_uri_setting('STATIC_URL')
|
||||
|
||||
|
||||
class GetMediaPrefixExtension(PrefixExtension):
|
||||
"""
|
||||
Populates a template variable with the media prefix,
|
||||
``settings.MEDIA_URL``.
|
||||
|
||||
Usage::
|
||||
|
||||
{% get_media_prefix [as varname] %}
|
||||
|
||||
Examples::
|
||||
|
||||
{% get_media_prefix %}
|
||||
{% get_media_prefix as media_prefix %}
|
||||
|
||||
"""
|
||||
|
||||
tags = set(['get_media_prefix'])
|
||||
|
||||
def render(self):
|
||||
return self.get_uri_setting('STATIC_URL')
|
||||
|
||||
|
||||
class StaticExtension(PrefixExtension):
|
||||
"""
|
||||
Joins the given path with the STATIC_URL setting.
|
||||
|
||||
Usage::
|
||||
|
||||
{% static path [as varname] %}
|
||||
|
||||
Examples::
|
||||
|
||||
{% static "myapp/css/base.css" %}
|
||||
{% static variable_with_path %}
|
||||
{% static "myapp/css/base.css" as admin_base_css %}
|
||||
{% static variable_with_path as varname %}
|
||||
|
||||
"""
|
||||
|
||||
tags = set(['static'])
|
||||
|
||||
def parse(self, parser):
|
||||
stream = parser.stream
|
||||
lineno = stream.next().lineno
|
||||
|
||||
path = parser.parse_expression()
|
||||
call_node = self.call_method('get_statc_url', args=[path])
|
||||
|
||||
if stream.next_if('name:as'):
|
||||
var = nodes.Name(stream.expect('name').value, 'store')
|
||||
return nodes.Assign(var, call_node).set_lineno(lineno)
|
||||
else:
|
||||
return nodes.Output([call_node]).set_lineno(lineno)
|
||||
|
||||
@classmethod
|
||||
def get_statc_url(cls, path):
|
||||
return urljoin(PrefixExtension.get_uri_setting("STATIC_URL"), path)
|
||||
|
||||
|
||||
register.tag(GetStaticPrefixExtension)
|
||||
register.tag(GetMediaPrefixExtension)
|
||||
register.tag(StaticExtension)
|
||||
|
||||
|
||||
def static(path):
|
||||
return StaticExtension.get_static_url(path)
|
Loading…
Reference in New Issue