Load styles and JS inline

This commit is contained in:
Martin Mahner 2018-03-24 08:56:50 +01:00
parent 336ae3b968
commit 54157aeda5
2 changed files with 48 additions and 0 deletions

View file

View file

@ -0,0 +1,48 @@
from __future__ import unicode_literals
import os
from logging import getLogger
from django.conf import settings
from django.contrib.staticfiles.finders import find
from django.template.defaulttags import register
from django.utils.safestring import mark_safe
logger = getLogger(__file__)
@register.simple_tag()
def inlinestatic(path):
"""
Similiar to Django's native `static` templatetag, but this includes
the file directly in the template, rather than a link to it.
Example:
<style type="text/css">{% inlinestatic "dpaste.css" %}</style>
<script>{% inlinestatic "dpaste.js" %}</script>
Becomes:
<style type="text/css">body{ color: red; }</style>
<script>alert("Hello World");</script>
Raises a ValueError if the the file does not exist, and
DEBUG is enabled.
:param path: (string) Filename of the file to include.
:return: (string) The included File or an empty string `''` if the
file was not found, and DEBUG is disabled.
"""
filename = find(path)
if not os.path.exists(filename):
logger.error('Unable to include inline static file "%s", '
'file not found.', filename)
if settings.DEBUG:
raise ValueError('Unable to include inline static file "{0}", '
'file not found.'.format(filename))
return ''
return mark_safe(open(filename).read())