PHPackages                             dpolac/twig-only-once - 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. dpolac/twig-only-once

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

dpolac/twig-only-once
=====================

Twig test which returns true only once per value.

v1.0.0(10y ago)021MITPHPPHP &gt;=5.3.0

Since Apr 30Pushed 10y ago1 watchersCompare

[ Source](https://github.com/dpolac/twig-only-once)[ Packagist](https://packagist.org/packages/dpolac/twig-only-once)[ RSS](/packages/dpolac-twig-only-once/feed)WikiDiscussions master Synced yesterday

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

OnlyOnce Twig Extension
=======================

[](#onlyonce-twig-extension)

Twig test which returns true only once per value.

Made to aid in looping through unique values.

```
    {% for article in articles if article.category is onlyOnce %}
        * {{ article.category }}
    {% endfor %}
```

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

[](#installation)

- Install via Composer:

```
    composer require dpolac/twig-only-once
```

- Add the extension to Twig:

```
    $twig->addExtension(new \DPolac\OnlyOnce\OnlyOnceExtension());
```

---

Usage
-----

[](#usage)

Extension provides two tests `onlyOnce` and `onlyOnceWhenOccurs`.

\###`is onlyOnce`

```
    {% if value is onlyOnce %} ... {% endif %}
    {% if value is onlyOnce(space) %} ... {% endif %}
```

Test returns true only when it's called for the first time with given `value` and `space` pair.

#### Arguments

[](#arguments)

- **value**: can be value of any type
    - number - is converted to string, so `12` and `'12'` are treated as same value,
    - string,
    - object - are compared by reference, so `new \stdClass()` and `new \stdClass()` are treated as different value,
    - array - to arrays are treated as same value when have exactly same key-value set; order of keys in hashes is ignored,
- **space**: can be any string (default: `'default'`)

\###`is onlyOnceWhenOccurs`

```
    {% if value is onlyOnceWhenOccurs(n) %} ... {% endif %}
    {% if value is onlyOnceWhenOccurs(n, space) %} ... {% endif %}
```

Test returns true only when it's called for the n-th time with given `value` and `space` pair. Note that `onlyOnce` is an alias to `onlyOnceWhenOccurs(1)`, so they share internal occurrences' counter.

#### Arguments

[](#arguments-1)

- **value**: as in `onlyOnce`,
- **n**: number of occurrences after which test returns true,
- **space**: can be any string (default: `'default'`).

Examples
--------

[](#examples)

Check tests in `./Tests/Fixtures/` directory for more examples.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

3713d ago

### Community

Maintainers

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

---

Top Contributors

[![dpolac](https://avatars.githubusercontent.com/u/18581845?v=4)](https://github.com/dpolac "dpolac (1 commits)")

---

Tags

phptwigfilteruniquedistinct

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dpolac-twig-only-once/health.svg)

```
[![Health](https://phpackages.com/badges/dpolac-twig-only-once/health.svg)](https://phpackages.com/packages/dpolac-twig-only-once)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.9k](/packages/craftcms-cms)[dpolac/twig-lambda

Lambda expressions for Twig and filters that make use of them

45208.4k1](/packages/dpolac-twig-lambda)

PHPackages © 2026

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