PHPackages                             machinateur/twig-context-tags - 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. machinateur/twig-context-tags

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

machinateur/twig-context-tags
=============================

A twig extension to enable the use of context tags in templates.

1.0.0(1y ago)06PHPPHP &gt;=8.1.0

Since Mar 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/machinateur/twig-context-tags)[ Packagist](https://packagist.org/packages/machinateur/twig-context-tags)[ Docs](https://github.com/machinateur/twig-context-tags)[ RSS](/packages/machinateur-twig-context-tags/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Twig Context Tags
=================

[](#twig-context-tags)

A [twig](https://github.com/twig/twig) extension that allows the following language construct in twig templates:

```
{% tag 'some-context-tag', 'some-other-context-tag' %}

```

This can be used to collect all tags defined when parsing and saving them to a custom `Template` method at compile-time, which in turn makes it possible to predict which context is required for the respective template at runtime, before rendering the actual contents.

This especially helps in a `1:1` route to template-file kinds of situations.

Requirements
------------

[](#requirements)

- PHP `>=8.1.0`
- Twig `^3.15`

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

[](#installation)

Use composer to install this twig extension.

```
composer require machinateur/twig-context-tags
```

Usage
-----

[](#usage)

This twig extension allows for defining certain context tags in template code, which will be evaluated at compile-time, but is available at runtime *before* actually rendering the content.

This mechanic allows for quite creative implementations, especially when combined with a content resolution framework.

The tags accept a wrapped or unwrapped sequence (i.e. with/without `[]`). Use it like this:

```
{% tag 'some-context-tag', 'some-other-context-tag' %}
{% tag ['the-third-tag'] %}
```

### Limitations

[](#limitations)

All tag values have to be constant expressions. This is inherent to how the extension works under the hood.

### Example

[](#example)

```
$environment = new \Twig\Environment(/*...*/);
$environment->addExtension(new \Machinateur\Twig\Extension\ContextTagExtension());
/** @var \Twig\Template&\Machinateur\Twig\TaggedTemplateInterface $template */
$template    = $environment->load($view)
    ->unwrap();

$tags        = $template->getContextTags();

dump($tags);
```

This will output something like this:

```
Array (
  0 => 'some-context-tag',
  1 => 'some-other-context-tag',
  2 => 'the-third-tag',
)
```

Tags are ordered by count and sequence of occurrence.

Then, after processing, to render the template:

```
echo $template->render([
    // ...
]);
```

### Template inheritance

[](#template-inheritance)

The extension also supports collecting tags across template inheritance boundaries, similar to how the `set` tag works. The parent tags will be prepended, if any.

The feature is turned off by default, but by passing `true` to `getContextTags()` it can be activated.

```
$tags        = $template->getContextTags(true);
// ...
```

### Note on potential abstractions

[](#note-on-potential-abstractions)

This pairs well with events, for example through [Symfony's EventDispatcher component](https://symfony.com/doc/current/components/event_dispatcher.html)in order to resolve the specific context entries by tag, before passing it to `render($context)`.

In such a setup, the responsibility to define context is shifted to the template designer and domain. At least as long as the underlying PHP application code can handle this kind of "dynamic context" resolution.

Tests
-----

[](#tests)

Run tests using the composer script:

```
composer test
```

License
-------

[](#license)

It's MIT.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance47

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

420d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2ac6a840f958d6940c5e2486c0ccc116f280f0f2ef3790b15f986b565a48459c?d=identicon)[machinateur](/maintainers/machinateur)

---

Top Contributors

[![machinateur](https://avatars.githubusercontent.com/u/81334941?v=4)](https://github.com/machinateur "machinateur (12 commits)")

---

Tags

phptwigextensioncontext-tag

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/machinateur-twig-context-tags/health.svg)

```
[![Health](https://phpackages.com/badges/machinateur-twig-context-tags/health.svg)](https://phpackages.com/packages/machinateur-twig-context-tags)
```

###  Alternatives

[nochso/html-compress-twig

Twig extension for compressing HTML and inline CSS/Javascript

84468.6k8](/packages/nochso-html-compress-twig)[goetas/twital

PHP templating engine that combines Twig and PHPTal power points

12813.1k1](/packages/goetas-twital)[redant/twig-components

Define reusable components in Twig.

3819.8k1](/packages/redant-twig-components)[goetas/twital-bundle

Twital templating engine Symfony Bundle

206.8k](/packages/goetas-twital-bundle)

PHPackages © 2026

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