PHPackages                             guym4c/twig-classnames - 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. guym4c/twig-classnames

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

guym4c/twig-classnames
======================

classnames() for Twig

v1.0.1(6y ago)1118MITPHP

Since Jun 3Pushed 6y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

`twig-classnames`
=================

[](#twig-classnames)

PHP port of [JedWatson/classnames](https://github.com/JedWatson/classnames) - a utility to conditionally construct CSS class names.

Install
-------

[](#install)

Add the dependency using Composer:

```
composer require guym4c/twig-classnames
```

And add to your Twig environment:

```
$twigEnv->addExtension(new Guym4c\TwigClassnames());
```

Usage
-----

[](#usage)

`classnames()`, called from your templates, takes any number of string or hash parameters. If the value associated with a given hash key is falsy, that key won't be included in the output.

```
{{ classNames('foo', 'bar') }} {# => 'foo bar' #}
{{ classNames('foo', { bar: true }) }} {# => 'foo bar' #}
{{ classNames({ 'foo-bar': true }) }} {# => 'foo-bar' #}
{{ classNames({ 'foo-bar': false }) }} {#  => '' #}
{{ classNames({ foo: true }, { bar: true }) }} {# => 'foo bar' #}
{{ classNames({ foo: true, bar: true }) }} {#  => 'foo bar' #}

{# lots of arguments of various types #}
{{ classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }) }}
{# => 'foo bar baz quux' #}

{# other falsy values are just ignored #}
{{ classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, '') }}
{# => 'bar 1' #}
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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

Every ~5 days

Total

2

Last Release

2207d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2103489?v=4)[Guy Mac](/maintainers/guym4c)[@guym4c](https://github.com/guym4c)

---

Top Contributors

[![guym4c](https://avatars.githubusercontent.com/u/2103489?v=4)](https://github.com/guym4c "guym4c (2 commits)")

---

Tags

twigclassnamesclsx

### Embed Badge

![Health badge](/badges/guym4c-twig-classnames/health.svg)

```
[![Health](https://phpackages.com/badges/guym4c-twig-classnames/health.svg)](https://phpackages.com/packages/guym4c-twig-classnames)
```

###  Alternatives

[timber/timber

Create WordPress themes with beautiful OOP code and the Twig Template Engine

5.7k3.6M127](/packages/timber-timber)[twig/intl-extra

A Twig extension for Intl

36667.2M319](/packages/twig-intl-extra)[symfony/ux-twig-component

Twig components for Symfony

21917.2M304](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1636.5M113](/packages/symfony-ux-live-component)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22919.7M81](/packages/twig-cssinliner-extra)[twig/inky-extra

A Twig extension for the inky email templating engine

16713.2M70](/packages/twig-inky-extra)

PHPackages © 2026

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