mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-15 08:02:54 +11:00
Load styles and JS inline
This commit is contained in:
parent
336ae3b968
commit
54157aeda5
2 changed files with 48 additions and 0 deletions
0
dpaste/templatetags/__init__.py
Normal file
0
dpaste/templatetags/__init__.py
Normal file
48
dpaste/templatetags/dpaste_tags.py
Normal file
48
dpaste/templatetags/dpaste_tags.py
Normal 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())
|
||||
|
Loading…
Reference in a new issue