GithubHelp home page GithubHelp logo

nochso / html-compress-twig Goto Github PK

View Code? Open in Web Editor NEW
80.0 5.0 17.0 22 KB

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

License: Other

PHP 100.00%
twig twig-extension compression minify minifier composer

html-compress-twig's Introduction

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.

html-compress-twig's People

Contributors

nochso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

html-compress-twig's Issues

Problem with PHP 7.4 and the solution

There is a problem of compatibility with php 7.4, it is in the file: plugins\dmitrybykov\htmlminify\vendor\matthiasmullie\minify\src\CSS.php

The problem is in the line 528 in the implode, in php 7.4 you need to change the position of the glue element and the array, so change: '/(?<=[: ])('.implode(array_keys($colors), '|').')(?=[; }])/i',
for: '/(?<=[: ])('.implode('|', array_keys($colors)).')(?=[; }])/i',

PhpUnit - Remaining deprecation notices

Hi,

Thanks for this extension ๐Ÿ‘

Symfony : 2.8.12
PhpUnit : 5.6.1

Phpunit returns this error:
"Referencing the "nochso\HtmlCompressTwig\Extension" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead:"

I think you just have to :

  • remove the getName() in Extension.php line 69
  • call the extension like : Extension::class in MinifyHtmlNode.php line 28

;-)

Vinz

Allow one space

Hi,

it would be must to be able :

<ul> <ul>     =>     </ul> </ul>   # Keep because one space
<ul>  <ul>    =>     </ul></ul>   # Compress because more one space

Thank you for job ;)

UTF-8 Support

Hello,

I'm just using your bundle since few days, and I see when I compress my google map javascript that the UTF-8 are broken...

Is there any thing I forgot to do ?

Thanks

Deprecation warninng

Getting deprecation warning:

Referencing the "HtmlCompressTwig" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.

Line 28 of MinifyHtmlNode.php is:
->write('$extension = $this->env->getExtension(\'' . Extension::NAME . '\');' . "\n")

Needs to be something like this:
->write('$extension = $this->env->getExtension(\'nochso\HtmlCompressTwig\Extension\');' . "\n")

I made this change to local copy and deprecation warning was gone.

or Extension::NAME constant can be changed to 'nochso\HtmlCompressTwig\Extension'

Current twig version:
twig/extensions v1.4.1
twig/twig v1.31.0

Thanks, Dave

All rules that uses custom CSS properties is removed

Input:

{% htmlcompress %}
<style>
.foo {
    --a: 1em;
    padding: var(--a);
    color: blue;
}
</style>
{% endhtmlcompress %}

Expected output:

<style>.foo {--a: 1em;padding: var(--a);color: blue;}</style>

Actual output:

<style>.foo {--a: 1em;color: blue;}</style>

Twig 3 Compatibility

Hi @nochso

I'm using your html-compress-twig for a few years and like it very much. I see it doesn't get much updates, but it works well, so it's fine :)

However, I noticed it doesn't work with Twig 3, which was released recently. Do you have any plans to support it? If not, maybe you can refer to some other maintained plugin which does that?

Thank you.

compression escape + javascript - problem

I have a javascript added to the template and function is doing wrong minification:

$('#auto').append('<tr id="auto_'+data.auto_id+'}}"><td>'+data.auto_movie+'</td><td><div class="form-check checkbox-slider--a checkbox-slider-md"><label>....

basically it sees it as HTML.

I tried to escape compression but I am getting error 'endhtmlcompress' is not found when inside template I put it twice.

start file here

{% htmlcompress %}
(include) HTML to be compressed
{% endhtmlcompress %}
(include) HTML NOT TO BE COMPRESSED
{% htmlcompress %}
(include) REST OF HTML TO BE COMPRESSED
{% endhtmlcompress %}

Any Idea how to work it out ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.