Skip to main content

Django template tag to load static files inline with your template.

Project description

https://travis-ci.org/bartTC/django-staticinline.svg?branch=master https://api.codacy.com/project/badge/Coverage/8e64345e99ea49888dc1bd9303c89a35 https://api.codacy.com/project/badge/Grade/8e64345e99ea49888dc1bd9303c89a35

django-staticinline

Similiar to Django’s native static templatetag, but this includes the file directly in the template, rather than a link to it.

Example:

{% load staticinline %}

<style type="text/css">{% staticinline "myfile.css" %}</style>
<script>{% staticinline "myfile.js" %}</script>

Becomes:

<style type="text/css">body{ color: red; }</style>
<script>alert("Hello World");</script>

If the file does not exist, and DEBUG is False, an empty string is returned and a error logfile is set. In case DEBUG is True, a ValueError is raised.

Installation

This Django app is compatible with Django 1.8 → 2.0, Python 2.7 all versions of Python 3.x.

Install the app with pip and add staticinline.apps.StaticInlineAppConfig to your installed apps in your settings.py:

pip install django-staticinline
INSTALLED_APPS = [
    # ...
    'staticinline.apps.StaticInlineAppConfig',
]

Encoder and Customization

You can automatically convert the file with the encode argument. django-staticinline ships with two encoders: base64 that transforms the file content to a base64 encoded string, and data that transforms the content into a data URI for use in CSS url() and HTML src="" attributes.

base64 encoder

{% load staticinline %}
{% staticinline "mykey.pem" encode="base64" %}'

Becomes:

LS0tIFN1cGVyIFByaXZhdGUgS2V5IC0tLQo=

data encoder

{% load staticinline %}
ul.checklist li.complete {
    background: url('{% staticinline "icons/check.png" encode="data" %}');
}

Becomes:

ul.checklist li.complete {
    background: url('data:image/png;base64,iVBORw0KG\
goAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD//\
/+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83ND\
DeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQ\
mCC');
}

Custom filters

You can add custom filters by setting them in a custom AppConfig. See the default AppConfig in staticinline/apps.py for further documentation. The test suite also uses a custom AppConfig, which will help you to understand the setup. See staticinline/tests/testapp/apps.py for it.

Changelog

v1.1 (2018-08-09)

  • Support for custom data encoders to modify file content on the fly.

  • Adds data and base64 encoders, both convert data into base64.

v1.0 (2018-04-29)

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-staticinline-1.1.tar.gz (11.2 kB view hashes)

Uploaded Source

Built Distribution

django_staticinline-1.1-py2.py3-none-any.whl (12.1 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page