This package is abandoned and no longer maintained. The author suggests using the twig/cssinliner-extra package instead.

A Twig extension to allow inlining CSS

v2.0.0 2019-02-24 09:17 UTC

This package is auto-updated.

Last update: 2019-10-17 14:00:05 UTC


WARNINIG: This package is deprecate; migrate to twig/cssinliner-extra instead.

This package provides a CSS inliner filter (inline_css) for Twig and a Symfony bundle.

If you are not using Symfony, register the extension on Twig's Environment manually:

use Twig\CssInliner\CssInlinerExtension;
use Twig\Environment;

$twig = new Environment(...);
$twig->addExtension(new CssInlinerExtension());

Use the inline_css filter from a Twig template:

{% filter inline_css %}
                p { color: red; }
            <p>Hello CSS!</p>
{% endfilter %}

You can also add some stylesheets by passing them as arguments to the filter:

{% filter inline_css(source("some_styles.css"), source("another.css")) %}
            <p>Hello CSS!</p>
{% endfilter %}

Styles loaded via the filter override the styles defined in the <style> tag of the HTML document.

You can also use the filter on an included file:

{{ include('some_template.html.twig')|inline_css }}

{{ include('some_template.html.twig')|inline_css(source("some_styles.css")) }}

Note that the CSS inliner works on an entire HTML document, not a fragment.