PHPackages                             studiometa/twig-toolkit - 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. studiometa/twig-toolkit

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

studiometa/twig-toolkit
=======================

A set of useful extension and components for Twig.

2.1.1(1y ago)1037.8k↓58.5%[2 issues](https://github.com/studiometa/twig-toolkit/issues)2MITPHPPHP ^8.1CI passing

Since Jan 31Pushed 1y ago9 watchersCompare

[ Source](https://github.com/studiometa/twig-toolkit)[ Packagist](https://packagist.org/packages/studiometa/twig-toolkit)[ RSS](/packages/studiometa-twig-toolkit/feed)WikiDiscussions develop Synced 3d ago

READMEChangelog (10)Dependencies (7)Versions (23)Used By (2)

studiometa/twig-toolkit
=======================

[](#studiometatwig-toolkit)

[![Packagist Version](https://camo.githubusercontent.com/8b97dee3aaadb59748daa9d6039d70ef05b2c15a1b2a502b56b9e26eb6caeda2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533266c6162656c3d7061636b6167697374)](https://packagist.org/packages/studiometa/twig-toolkit)[![Downloads](https://camo.githubusercontent.com/3e37938b18ce07ecba938cc1f71e8a0d6dd0fd548a0d830ff0af67f7fbc3b36a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533)](https://packagist.org/packages/studiometa/twig-toolkit)[![Dependency Status](https://camo.githubusercontent.com/07e7dbb0f7884f24340ee60478d19a156cb88fdd260d97945fe15beebb0c40d0/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f6769746875622f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533)](https://packagist.org/packages/studiometa/twig-toolkit)[![Codecov](https://camo.githubusercontent.com/d56da959cc30670536c9d2bbfdff8ba7d7d61778640b45bbfaa99776e132a4a8/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533)](https://camo.githubusercontent.com/d56da959cc30670536c9d2bbfdff8ba7d7d61778640b45bbfaa99776e132a4a8/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533)[![License MIT](https://camo.githubusercontent.com/db54a58bc54f0b8e528c3b68d3d6b6d268470878b36c16c5f948873ccfaa5913/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73747564696f6d6574612f747769672d746f6f6c6b69743f7374796c653d666c617426636f6c6f72423d33653633646426636f6c6f72413d343134383533)](https://github.com/studiometa/twig-toolkit/blob/master/LICENSE)

> A set of useful extension and components for Twig.

Installation
------------

[](#installation)

```
composer require studiometa/twig-toolkit
```

Usage
-----

[](#usage)

Add the `Studiometa\TwigToolkit\Extension` to your Twig instance:

```
$loader = new \Twig\Loader\FilesystemLoader();
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Studiometa\TwigToolkit\Extension());
```

Reference
---------

[](#reference)

### Functions

[](#functions)

#### `{{ html_classes() }}`

[](#-html_classesclasses-)

A function to manage classes more easily.

**Params**

- `classes` (`String | Array | Object`)

**Examples**

```
{# The following examples will render the same HTML #}

{# HTML #}

```

#### `{{ html_styles() }}`

[](#-html_stylesstyles-)

A function to manage style attributes more easily.

**Params**

- `styles` (`Object`)

**Examples**

```

```

#### `{{ html_attributes() }}`

[](#-html_attributesattrs-)

A function to render HTML attributes more easily with the following features:

- The `class` attribute will automatically be processed by the `class` method described above
- Array and objects will be converted to JSON
- Attributes keys will be converted from any format to kebab-case
- Values will be escaped to prevent XSS attacks

**Params**

- `attrs` (`Object`): The attributes to render

**Examples**

```

{# HTML #}

```

#### `{{ merge_html_attributes(attr, default_attr, required_attr) }}`

[](#-merge_html_attributesattr-default_attr-required_attr-)

Merge HTML attributes smartly, useful to define default and required attributes at the component level and allow users to add custom ones.

This filter can also be used as a function.

**Params**

- `attr` (`Object`): The user provided attributes
- `default` (`Object`): The default attributes
- `required` (`Object`): The required attributes

**Examples**

You can define default and required attributes in a component's template:

```
{#
/**
 * @file
 * component
 *
 * @param array $attr
 *   Custom attributes to apply to the root element.
 */
#}

{% set default_attributes = { class: 'bar' } %}
{% set required_attributes = { data_component: 'Component' } %}

{# Merge all attributes #}
{% set attributes = merge_html_attributes(attr, default_attributes, required_attributes)}

{# or #}
{% html_element 'div' with attributes %}
```

And then include your component with custom attributes:

```
{% include 'component.twig' with {
  attr: {
    class: 'mb-10',
    aria_hidden: 'true'
  }
} %}
```

You can take advantage of [named arguments](http://twig.symfony.com/doc/3.x/templates.html#named-arguments) to avoid passing a value for the default attributes argument:

```
{% set required_attributes = { id: 'block' } %}
{% set merged_attributes = merge_html_attributes(attr, required=required_attribute) %}
```

#### `{{ twig_toolkit_url(string) }}`

[](#-twig_toolkit_urlstring-)

Wrapper for the `Spatie\Url\Url` class to easily manipulate URLs. See the [`spatie/url` documentation](https://github.com/spatie/url) for details on its API.

**Params**

- `url` (`string`): The URL to parse for manipulation

**Examples**

```
{# Change host #}
{% set url = twig_toolkit_url(url).withHost('cdn.fqdn.com') %}

{# Change/add query parameters #}
{% set url = twig_toolkit_url(url).withQueryParameter('key', 'value') %}
```

### Filters

[](#filters)

#### `{{ object|twig_toolkit_without('key', 'other_key') }}`

[](#-objecttwig_toolkit_withoutkey-other_key-)

Returns the given hash without the specified keys.

**Params**

- `value` (`array`): the hash to remove the key from
- `...keys` (`string[]`): the keys to remove

**Examples**

```
{# Twig #}
{{ { foo: 1, bar: 2, baz: 3 }|twig_toolkit_without('bar')|keys|join(', ') }}

{# HTML #}
foo, baz
```

### Tags

[](#tags)

#### `{% html_element '' with attrs %}`

[](#-html_element-tag-with-attrs-)

Render an HTML element with the given attributes. Useful to avoid setting dynamic HTML element tags with the `...` pattern.

**Params**

- `tag` (`String`): The name of the tag
- `attrs` (`Object`): An object describing the element's attribues

**Examples**

```
{# Twig #}
{% html_element 'h1' with { class: 'block' } %}
  Hello world
{% end_html_element %}

{# HTML #}

  Hello world

```

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance44

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 93.1% 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 ~99 days

Recently: every ~66 days

Total

17

Last Release

380d ago

Major Versions

1.3.7 → 2.0.02025-01-20

PHP version history (3 changes)1.0.0PHP &gt;=7.0

1.1.0PHP ^7.3|^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e88435dae9fed57464c2668f35d4c7dd7f874e55e9727972369538d639d6373?d=identicon)[titouanmathis](/maintainers/titouanmathis)

---

Top Contributors

[![titouanmathis](https://avatars.githubusercontent.com/u/250145?v=4)](https://github.com/titouanmathis "titouanmathis (162 commits)")[![perruche](https://avatars.githubusercontent.com/u/11503190?v=4)](https://github.com/perruche "perruche (11 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (1 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/studiometa-twig-toolkit/health.svg)

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)

PHPackages © 2026

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