voku/html-compress-twig

Twig extension for compressing HTML

Installs: 1 303 497

Dependents: 0

Suggesters: 0

Security: 0

Stars: 23

Watchers: 4

Forks: 17

4.0.2 2021-01-05 20:03 UTC

This package is auto-updated.

Last update: 2024-03-04 01:46:51 UTC


README

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version License

🗜️ HtmlMin for Twig: HTML Compressor and Minifier

Description

A Twig extension for voku/HtmlMin.

Currently supported Twig features are:

  • Tag

    • {% htmlcompress %} <foo>bar</foo> {% endhtmlcompress %}
  • Function

    • {{ htmlcompress(' <foo>bar</foo>') }}
  • Filter

    • {{ ' <foo>bar</foo>' | htmlcompress }}
  • Installation

  • Usage

  • History

  • License

Installation

  1. Install and use composer in your project.
  2. Require this package via composer:
composer require voku/html-compress-twig

Usage

First register the extension with Twig:

use voku\helper\HtmlMin;
use voku\twig\MinifyHtmlExtension;

$twig = new \Twig\Environment($loader);
$minifier = new HtmlMin();
$twig->addExtension(new MinifyHtmlExtension($minifier));

Register extension in symfony 4

Specifying HtmlMin is needed for the autowiring.

    voku\helper\HtmlMin:
        tags:
            - { name: HtmlMin }

    voku\twig\MinifyHtmlExtension:
        arguments:
            $forceCompression: false
        tags:
            - { name: twig.extension }

Then use it in your templates:

{% htmlcompress %} <foo>bar</foo> {% endhtmlcompress %}
{{ htmlcompress(' <foo>bar</foo>') }}
{{ ' <foo>bar</foo>' | 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 as second parameter $forceCompression. When true, this will force compression regardless of Twig's debug setting. It defaults to false when omitted.

$twig->addExtension(new MinifyHtmlExtension($minifier, 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.