django-inline-static

Latest Version CI Status Coverage Status Documentation Status

django-inline-static provides template tags to inline css and javascript files in Django templates, also corrects relative urls in css “url()” calls.

Features

  • Template tag to inline any static file inline_staticfile

  • Template tag to inline javascript files inline_javascript

  • Template tag to inline css files inline_style

  • relative to absolute path converter for paths in css files

Requirements

django-inline-static supports Python 3 only and requires at least Django 1.11.

Prepare for development

A Python 3 interpreter is required in addition to poetry.

$ poetry install

Now you’re ready to run the tests:

$ make tests

Contents:

Installation

  • Install with pip:

    pip install django-inline-static
    
  • Your INSTALLED_APPS setting:

    INSTALLED_APPS = (
        # ...
        'inline_static',
    )
    

Usage

Loading static files

To inline any static file in your template, use the inline_staticfile tag. The inlined content is not marked safe, do this by yourself if you’re sure that the included content is safe for html documents.

{% load inline_static_tags %}

{% inline_staticfile 'build/fake.txt' %}

<!-- or -->

{% inline_staticfile 'build/image.svg' as image %}{{ image|safe }}

Inlining javascript

If you want to inline javascript code, use the inline_javascript tag. The inlined content will be marked as safe.

{% load inline_static_tags %}

<script>{% inline_javascript 'build/critical.pkg.js' %}</script>

Inlining styles

To inline a css styles file, use the inline_style template tag.

The styles will be transformed by replacing any relative url in the content with absolute urls to make sure fonts, background images and other url() calls work after including.

{% load inline_static_tags %}

<style type="text/css">{% inline_style 'build/all.css' %}</style>

Changelog

0.1.0 (2019-03-19)

  • Ensure that inline static included the hashed versions of a static file when filename hashing is available in the staticfiles storage.

0.0.1 (2018-08-08)

  • Initial release of django-inline-static

Api documentation:

inline_static.templatetags

inline_static.templatetags.inline_static_tags.inline_staticfile(name)[source]
inline_static.templatetags.inline_static_tags.inline_style(name)[source]
inline_static.templatetags.inline_static_tags.inline_javascript(name)[source]

inline_static.loader

inline_static.loader.load_staticfile(name, postprocessor=None, fail_silently=False)[source]

inline_static.css

class inline_static.css.CssUrlTransformer(name, path, content, base_url=None)[source]

Bases: object

SCHEMES = ('http://', 'https://', '/')[source]
URL_PATTERN = re.compile('\n        url\\(\n        \\s*      # any amount of whitespace\n        ([\\\'"]?) # optional quote\n        (.*?)    # any amount of anything, non-greedily (this is the actual url)\n        \\1     , re.VERBOSE)[source]
parse_static_url(base_url)[source]
transform()[source]
transform_url(match)[source]
resolve_url(url)[source]
inline_static.css.transform_css_urls(*args, **kwargs)[source]

Indices and tables