PHPackages                             nystudio107/minify - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. nystudio107/minify

ActiveCraft-plugin[Templating &amp; Views](/categories/templating)

nystudio107/minify
==================

A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates.

1.2.0(8y ago)1062.0k10[1 issues](https://github.com/nystudio107/minify/issues)PHP

Since Dec 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/nystudio107/minify)[ Packagist](https://packagist.org/packages/nystudio107/minify)[ RSS](/packages/nystudio107-minify/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (0)

[![No Maintenance Intended](https://camo.githubusercontent.com/d904056147052e22d8e1c7f46bb50293ed2aeb4c43ead9a2d0cf7a48b46d0562/687474703a2f2f756e6d61696e7461696e65642e746563682f62616467652e737667)](http://unmaintained.tech/)

DEPRECATED
==========

[](#deprecated)

This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.

The Craft CMS 3.x version of this plugin can be found here: [craft-minify](https://github.com/nystudio107/craft-minify) and can also be installed via the Craft Plugin Store in the Craft CP.

Minify plugin for Craft CMS
===========================

[](#minify-plugin-for-craft-cms)

A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates

Related: [Minify for Craft 3.x](https://github.com/nystudio107/craft3-minify)

**Installation**

1. Download &amp; unzip the file and place the `minify` directory into your `craft/plugins` directory
2. -OR- do a `git clone https://github.com/khalwat/minify.git` directly into your `craft/plugins` folder. You can then update it with `git pull`
3. -OR- install with Composer via `composer require nystudio107/minify`
4. Install plugin in the Craft Control Panel under Settings &gt; Plugins
5. The plugin folder should be named `minify` for Craft to see it. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads.

Configuring Minify
------------------

[](#configuring-minify)

There is nothing to configure

Using the Minify plugin in your templates
-----------------------------------------

[](#using-the-minify-plugin-in-your-templates)

Minify adds several block tags for minifying HTML, CSS, and JS inline in your templates. Minify does **not** minify external CSS or JS files. Use `grunt` or `gulp` task runners to set up a workflow that minimizes these as part of your build process.

You can nest any number of the various `{% minify %}` tags as you wish.

Why minify inline HTML/CSS/JS code?
-----------------------------------

[](#why-minify-inline-htmlcssjs-code)

Twig provides the `{% spaceless %}` tag, but it is not intended for use as a way to properly minify HTML/CSS/JS code.

You already properly `gzip` output via `mod_deflate` with Apache or by enabling compression with `Nginx` for optimal delivery on production. You already use a task runner like `grunt` or `gulp` or CodeKit to minimize static resources like CSS/JS files. What's the point of minifying HTML/CSS/JS inline?

Firstly, you want to keep HTML/CSS/JS comments and a nice hierarchical structure to your code, with plenty of readable whitespace for development, but want all of this stripped out of the HTML/CSS/JS that is served to your frontend.

Secondly, not all CSS/JS can or should be in static asset files. Sometimes you need inline Javascript for efficiency reasons, or if you're using `JSON-LD` for Google Structured Data/SEO purposes inline in your HTML files. You may also want to be able to use the Craft templating engine in your CSS/JS itself.

Finally, if you minify any code you wrap in `{% cache %}` tags, that means it will be stored minified in the database, reducing db size and (marginally) transactional overhead.

### Minifying Everything

[](#minifying-everything)

You can wrap any arbitrary HTML/Twig code in the following block tags to minify it:

```
{% minify %}
	(HTML/Twig code here)
{% endminify %}

```

...and the resulting HTML output will be stripped of comments, empty space, etc.

Using the `{% minify %}` on its own with no parameters (see below) will minify HTML, as well as any inline CSS (in `` tag pairs) and any inline Javascript (in `` tag pairs).

### Minifying HTML

[](#minifying-html)

You can wrap any arbitrary HTML/Twig code in the following block tags to minify it:

```
{% minify html %}
	(HTML/Twig code here)
{% endminify %}

```

...and the resulting HTML output will be stripped of comments, empty space, etc.

It will ignore `` and `` tag pairs in the minification. You should specifically wrap your inline CSS/JS in `{% minify css %}` and `{% minify js}` tag blocks if you want those minimized as well; see below.

### Minifying CSS

[](#minifying-css)

You can wrap any arbitrary `` CSS code in the following block tags to minify it:

```
{% minify css %}

		(Inline CSS styles here)

{% endminify %}

```

...and the resulting CSS output will be stripped of comments, empty space, etc.

### Minifying JS

[](#minifying-js)

You can wrap any arbitrary `` JS code in the following block tags to minify it:

```
{% minify js %}

		(Inline JS code here)

{% endminify %}

```

...and the resulting JS output will be stripped of comments, empty space, etc.

Minify all the things
---------------------

[](#minify-all-the-things)

If you want to minify your entire HTML on the frontend, you can simply wrap your entire `_layout.twig` template (the one that other templates `extends`):

```
{% minify %}
		(Entire base HTML/Twig template here)
{% endminify %}

```

However, understand the potential performance implications. On large HTML/CSS/JS blocks minification is not cheap, and if you do it this way, every single HTTP request will spend extra cycles minimizing your entire template.

It works fine for HTML/CSS/JS templates that aren't too huge, but measure any performance impact by profiling your website before and after wrapping the entire `_layout.twig` template to ensure you're not introducing additional latency in the http requests.

On most sites, the overhead that spinning up PHP and Craft takes is the majority of the TTFB (Time To First Byte), and this is not adversely affected by minifying the entire HTML as described here.

Cache as cache can
------------------

[](#cache-as-cache-can)

A great way to use the `{% minify %}` tags is to wrap them in `{% cache %}` tags:

```
{% cache %}
	{% minify %}
		(HTML/Twig code here)
	{% endminify %}
{% endcache %}

```

As with `{% cache %}` tags, you can’t use `{% minify %}` tags outside of top-level `{% block %}` tags within a template that extends another. [Read more about cache tags](http://buildwithcraft.com/docs/templating/cache)

A nice side-benefit of minifying HTML inside of `{% cache %}` tags is that the text that is stored in the database as a cache is minified itself.

If you've already implemented a caching system to reduce server response time, adding `{% minify %}` tags to the mix is a natural.

Minify craft.config settings
----------------------------

[](#minify-craftconfig-settings)

Minify offers two `craft.config` (set in your `config/general.php`) to allow you to control its behavior:

`disableTemplateMinifying` if set to `true` then Minify will not minify anything

`disableDevmodeMinifying` if set to `true` then Minify will not minify anything if `devMode` is enabled

Why does my minified HTML/CSS still have linebreaks?
----------------------------------------------------

[](#why-does-my-minified-htmlcss-still-have-linebreaks)

Minify uses the [Minify PHP library](https://github.com/mrclay/minify). Here's their official explanation:

#### Why do the CSS &amp; HTML minifiers add so many line breaks?

[](#why-do-the-css--html-minifiers-add-so-many-line-breaks)

TL;DR: Ignore them. They don't add to the output size and if you absolutely want all content on one line you will have to use another tool.

It's rumored that some source control tools and old browsers don't like very long lines. Compressed files with shorter lines are also easier to diff.

Since both Minify classes are regex-based, it would be very difficult/error-prone to count characters then try to re-establish context to add line breaks. Instead, both classes trade 1 space for 1 line break (\\n) wherever possible, adding breaks but without adding bytes.

If you can think of another safe &amp; efficient way to limit lines in these two tools without adding bytes, please submit a patch, but this is not something anyone should be worrying about.

Brought to you by [nystudio107](http://nystudio107.com)

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 96% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~127 days

Recently: every ~157 days

Total

6

Last Release

3199d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7570798?v=4)[Andrew Welch](/maintainers/khalwat)[@khalwat](https://github.com/khalwat)

---

Top Contributors

[![khalwat](https://avatars.githubusercontent.com/u/7570798?v=4)](https://github.com/khalwat "khalwat (24 commits)")[![brandonkelly](https://avatars.githubusercontent.com/u/47792?v=4)](https://github.com/brandonkelly "brandonkelly (1 commits)")

---

Tags

craft-plugincraftcmsdeprecatedminify

### Embed Badge

![Health badge](/badges/nystudio107-minify/health.svg)

```
[![Health](https://phpackages.com/badges/nystudio107-minify/health.svg)](https://phpackages.com/packages/nystudio107-minify)
```

###  Alternatives

[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.1k16.8k](/packages/prestashop-prestashop)[helsingborg-stad/municipio

A bootstrap theme for creating municipality sites.

4028.3k10](/packages/helsingborg-stad-municipio)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45344.0k1](/packages/pressbooks-pressbooks)[pressbooks/pressbooks-book

This theme is named after Canadian media theorist Marshall McLuhan, who coined the phrase “the medium is the message.” It is designed for academic writing and is also suitable for fiction. Headings are set in Cormorant Garamond, and body type is set in Lora.

206.7k](/packages/pressbooks-pressbooks-book)[silverstripe-themes/simple

The SilverStripe simple theme (default SilverStripe 3 theme)

411.3M8](/packages/silverstripe-themes-simple)[octfx/template-styles-extender

Extends TemplateStyles with new CSS properties

105.6k](/packages/octfx-template-styles-extender)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
