Html Compress Twig Save

Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress

Project README

nochso/html-compress-twig extension

Latest Stable Version License Build Status

A Twig extension for WyriHaximus/HtmlCompress.

Currently supported Twig features are:

  • Tag
    • {% htmlcompress %} ... {% endhtmlcompress %}
  • Function
    • {{ htmlcompress('some html') }}
  • Filter
    • {{ content|markdown|htmlcompress }}

Any HTML, inline CSS and Javascript will be compressed.

Installation

  1. Install and use composer in your project.

  2. Require this package via composer:

    composer require nochso/html-compress-twig
    

Usage

First register the extension with Twig:

$twig = new Twig_Environment($loader);
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());

Then use it in your templates:

{% htmlcompress %}{% endhtmlcompress %}
{{ htmlcompress('<ul> <li>') }}
{{ '<ul> <li>'|htmlcompress }}

Compression is disabled by Twig's debug setting. This is to make development easier, however you can always override it.

The constructor of this extension takes a boolean parameter $forceCompression. When true, this will force compression regardless of Twig's debug setting. It defaults to false when omitted.

// Enable compression regardless of Twig's debug setting
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension(true));

History

See CHANGELOG for the full history of changes.

License

This project is licensed under the ISC license which is MIT/GPL compatible and FSF/OSI approved. See the LICENSE file for the full license text.

Open Source Agenda is not affiliated with "Html Compress Twig" Project. README Source: nochso/html-compress-twig
Stars
80
Open Issues
7
Last Commit
4 years ago

Open Source Agenda Badge

Open Source Agenda Rating