From 54157aeda5aa9ce673490d7e7f0541ba2ce93be4 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Sat, 24 Mar 2018 08:56:50 +0100 Subject: [PATCH] Load styles and JS inline --- dpaste/templatetags/__init__.py | 0 dpaste/templatetags/dpaste_tags.py | 48 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 dpaste/templatetags/__init__.py create mode 100644 dpaste/templatetags/dpaste_tags.py diff --git a/dpaste/templatetags/__init__.py b/dpaste/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dpaste/templatetags/dpaste_tags.py b/dpaste/templatetags/dpaste_tags.py new file mode 100644 index 0000000..cd9f7a2 --- /dev/null +++ b/dpaste/templatetags/dpaste_tags.py @@ -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: + + + + + Becomes: + + + + + 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()) +