forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed django#18254 -- Added ability to the static template tags to st…
…ore the result in a contextt variable. Many thanks to Andrei Antoukh for the initial patch.
- Loading branch information
Showing
6 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters