PHPackages                             webapper/twigassets - 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. webapper/twigassets

ActiveLibrary[Templating &amp; Views](/categories/templating)

webapper/twigassets
===================

TwigAssets - on-the-fly assets manager for Twig template engine

1.0.x-dev(12y ago)012Creative Commons 4.0PHP

Since Apr 1Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Webapper/TwigAssets)[ Packagist](https://packagist.org/packages/webapper/twigassets)[ RSS](/packages/webapper-twigassets/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (2)Used By (0)

TwigAssets
==========

[](#twigassets)

Implements an on-the-fly asset manager for Twig

This is a part of a light-weight framework, Prometheus: (language only in hungarian, sorry)

[![Creative Commons License](https://camo.githubusercontent.com/d1a392e365cf029e2205bab9b7182c337e94efaddd1d0ad315617843d71cd9cc/687474703a2f2f692e6372656174697665636f6d6d6f6e732e6f72672f6c2f62792f342e302f38387833312e706e67)](http://creativecommons.org/licenses/by/4.0/)
TwigAssets by [Assarte D'Raven](http://webapper.vallalatiszolgaltatasok.hu/#!/prometheus) is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).

How you help me
---------------

[](#how-you-help-me)

Feel free to use my extension, I hope that you may enjoy that and may help you on your better efficiency. Well, you should donate me some credits via PayPal if my help counts for you on your work:

[![PayPal - The safer, easier way to pay online!](https://camo.githubusercontent.com/e1ff554a09e8e92bef25abc553ff05b88f45afd695877cf12f3a46558ef65b2e/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e61746543435f4c472e676966)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5KQ66J5DF97RA)...or give me some positive feedback on my e-mail adress (you can see that in my profile).

Thanks anyway!

How it works
------------

[](#how-it-works)

You should change Twig's `base_template_class` option value to: `'Assarte_TwigAssets_Template'`. This is highly recommended.

```
$loader = new Twig_Loader_Filesystem('path/to/templates');
$env = new Twig_Environment($loader, array(
	'cache'					=> 'path/to/compiled/sources',
	'base_template_class'	=> 'Assarte_TwigAssets_Template'
));
```

You must add the `Assarte_TwigAssets_Extension_Assets` extension to Twig.

```
$env->addExtension(
	new Assarte_TwigAssets_Extension_Assets(array(
		'namespace'			=> Assarte_TwigAssets_Storage_Filesystem::STORE_NAMESPACE,
		'storage'			=> new Assarte_TwigAssets_Storage_Filesystem(
			$loader,
			'path/to/public/assets/'
		),
		'name_generator_cb'	=> 'md5' // You may use any type of callbacks
	))
);
```

That's all in your PHP code!

How to use
----------

[](#how-to-use)

You've got four new *Tags* and one *Function* for Twig:

- `{{ asset_empty('collection-name') }}`: This function checks if an asset collection is empty/unexistant or not. You should use it in conditional places if you not want to include noise-like empty CSS or JS files on your HTML code.
- `{% asset 'path/to/asset.file' bind 'collection-name' %}`: This indicates that a template requires an asset. You can use and reuse many assets as you want and where you want. You can bind any assets to any collections. You can name any collections as you want. All assets in an exact collection will be unique even if you require more than once.
- `{% build 'collection-name' as 'css|js' [no_minify] %}...{% endbuild %}`: **The new way of building a collection!** A `build` block's contents displayed only if collection has some - one at least - asset. Use the new `use_asset` tag within to display the filename of builded asset collection. This way is more efficent if you want optionally include a collection of assets based on that if it has assets or not. You can place a `build` block before the adding of any assets to the `build`ed collection (you cannot do this with the `if not asset_empty('collection-name')`-way)!
- `{% use_asset 'collection-name' %}`: Displays an asset collection's filename within a `build` block. For example:

    ```
     {% build 'default-css' as 'css' %}

     {% endbuild %}

    ```
- `{% asset_build 'collection-name' as 'css|js' [no_minify] %}`: **This is the old way of building and placeing a collection.** You should use this if you sure about that the collection always contains one or more assets. This tag indicates a place where a collection of assets needs to be used. Here you must specify the type of the specific collection (`'js'` and `'css'` supported by default). You can control the minifing of assets with the optional `no_minify` switch. For example:

    ```

    ```

How to minify
-------------

[](#how-to-minify)

You should use the Extension's `minify` option to indicate if you want to minify your asset-collections (`true`) or not (`false`). If you indicates that you want to use minifing you must setup a minifier-callback by the `minifier_callback` option. The minifier callback must have two arguments: `string $content, string $type`. This callback must returns the minified version of the passed `$content`'s content. An example of usage:

```
$env->addExtension(
	new Assarte_TwigAssets_Extension_Assets(array(
		...,
		'minifing'			=> true,
		'minifier_callback'	=> function($content, $type) {
			switch ($type) {
				default: {
					throw new RuntimeException('Invalid asset minifier type: '.$type);
					break;
				}
				case 'js': {
					$result = JSMinPlus::minify($content);
					return $result;
				}
				case 'css': {
					$minifier = new CSSmin();
					$result = $minifier->run($content);
					return $result;
				}
			}
		}
	))
);
```

An interesting tip: you can use minifier\_callback to replace URLs in your assets with framework controlled addresses.

```
$env->addExtension(
	new Assarte_TwigAssets_Extension_Assets(array(
		...,
		'minifing'			=> true,
		'minifier_callback'	=> function($content, $type) {
			$content = preg_replace_callback(
				'#/\*\s+@url\s+([^\*]+)\s+\*/#i', function($matches) use ($app) {
					return my_frameworks_url_generator($matches[1], true);
				}, $content
			);
		}
	))
);
```

The code above can replace a CSS rule like this:

```
#myCoolDiv {
	background: url(/* @url /my/magical/background.jpg */);
}
```

...to something like this...

```
#myCoolDiv {
	background: url(/index.php?trickey_image_watermarker=/my/magical/background.jpg);
}
```

It's simple as hell. Anyway, you should grab these libs with ease:

- JSMinPlus:
- CSSmin:

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

4427d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a84c693d6fcce961c9953932737eafb748c6db09d1a5ae5e35a5eb89fd037629?d=identicon)[assarte](/maintainers/assarte)

---

Top Contributors

[![assarte](https://avatars.githubusercontent.com/u/5679362?v=4)](https://github.com/assarte "assarte (42 commits)")

---

Tags

twigassets

### Embed Badge

![Health badge](/badges/webapper-twigassets/health.svg)

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M221](/packages/twig-intl-extra)[twig/string-extra

A Twig extension for Symfony String

21946.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M162](/packages/symfony-ux-twig-component)[twig/markdown-extra

A Twig extension for Markdown

12114.3M83](/packages/twig-markdown-extra)

PHPackages © 2026

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