diff options
| author | Florian Apolloner <florian@apolloner.eu> | 2012-07-07 15:49:00 +0200 |
|---|---|---|
| committer | Florian Apolloner <florian@apolloner.eu> | 2012-07-07 15:49:00 +0200 |
| commit | a4bb7dd552cf9e45cdd0b38938b18c576419fd8d (patch) | |
| tree | 4efc2b8368cb9b8eb446924f482072ead44c5c9b /django | |
| parent | 52a9e15794ac050afb17837a34407722e7249854 (diff) | |
| parent | 0a68a2994be17ed2669accead331881cb0be41dd (diff) | |
Merge branch 'master' of github.com:django/django
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/staticfiles/templatetags/staticfiles.py | 30 | ||||
| -rw-r--r-- | django/templatetags/static.py | 63 |
2 files changed, 86 insertions, 7 deletions
diff --git a/django/contrib/staticfiles/templatetags/staticfiles.py b/django/contrib/staticfiles/templatetags/staticfiles.py index 788f06ec16..71339ea8cd 100644 --- a/django/contrib/staticfiles/templatetags/staticfiles.py +++ b/django/contrib/staticfiles/templatetags/staticfiles.py @@ -1,13 +1,37 @@ from django import template +from django.templatetags.static import StaticNode from django.contrib.staticfiles.storage import staticfiles_storage register = template.Library() -@register.simple_tag -def static(path): +class StaticFilesNode(StaticNode): + + def url(self, context): + path = self.path.resolve(context) + return staticfiles_storage.url(path) + + +@register.tag('static') +def do_static(parser, token): """ A template tag that returns 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 %} + """ - return staticfiles_storage.url(path) + return StaticFilesNode.handle_token(parser, token) + + +def static(path): + return StaticNode.handle_simple(path) diff --git a/django/templatetags/static.py b/django/templatetags/static.py index cba44378c3..4b2d66d521 100644 --- a/django/templatetags/static.py +++ b/django/templatetags/static.py @@ -1,9 +1,11 @@ from urlparse import urljoin from django import template +from django.template.base import Node from django.utils.encoding import iri_to_uri register = template.Library() + class PrefixNode(template.Node): def __repr__(self): @@ -48,6 +50,7 @@ class PrefixNode(template.Node): context[self.varname] = prefix return '' + @register.tag def get_static_prefix(parser, token): """ @@ -66,6 +69,7 @@ def get_static_prefix(parser, token): """ return PrefixNode.handle_token(parser, token, "STATIC_URL") + @register.tag def get_media_prefix(parser, token): """ @@ -84,19 +88,70 @@ def get_media_prefix(parser, token): """ return PrefixNode.handle_token(parser, token, "MEDIA_URL") -@register.simple_tag -def static(path): + +class StaticNode(Node): + def __init__(self, varname=None, path=None): + if path is None: + raise template.TemplateSyntaxError( + "Static template nodes must be given a path to return.") + self.path = path + self.varname = varname + + def url(self, context): + path = self.path.resolve(context) + return self.handle_simple(path) + + def render(self, context): + url = self.url(context) + if self.varname is None: + return url + context[self.varname] = url + return '' + + @classmethod + def handle_simple(cls, path): + return urljoin(PrefixNode.handle_simple("STATIC_URL"), path) + + @classmethod + def handle_token(cls, parser, token): + """ + Class method to parse prefix node and return a Node. + """ + bits = token.split_contents() + + if len(bits) < 2: + raise template.TemplateSyntaxError( + "'%s' takes at least one argument (path to file)" % bits[0]) + + path = parser.compile_filter(bits[1]) + + if len(bits) >= 2 and bits[-2] == 'as': + varname = bits[3] + else: + varname = None + + return cls(varname, path) + + +@register.tag('static') +def do_static(parser, token): """ Joins the given path with the STATIC_URL setting. Usage:: - {% static path %} + {% 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 %} """ - return urljoin(PrefixNode.handle_simple("STATIC_URL"), path) + return StaticNode.handle_token(parser, token) + + +def static(path): + return StaticNode.handle_simple(path) |
