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()) +