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

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

preflow/twig
============

Preflow Twig adapter — Twig template engine implementation

v0.13.1(1mo ago)0411MITPHPPHP &gt;=8.4

Since Apr 11Pushed 1mo agoCompare

[ Source](https://github.com/getpreflow/twig)[ Packagist](https://packagist.org/packages/preflow/twig)[ RSS](/packages/preflow-twig/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (19)Used By (1)

preflow/twig
============

[](#preflowtwig)

Twig 3 adapter for Preflow. Implements `TemplateEngineInterface` and ships co-located CSS/JS support via `{% apply css %}` / `{% apply js %}` filters and `{{ head() }}` / `{{ assets() }}` functions.

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

[](#installation)

```
composer require preflow/twig
```

Requires PHP 8.4+ and Twig 3.

What's included
---------------

[](#whats-included)

ComponentDescription`TwigEngine``TemplateEngineInterface` implementation wrapping `Twig\Environment``PreflowExtension``{% apply css %}`, `{% apply js %}`, `{{ head() }}`, `{{ assets() }}``ComponentExtension``{{ component('Name', {props}) }}` — renders Preflow components`HdExtension``{{ hd.post(...) }}`, `{{ hd.get(...) }}` — hypermedia action helpers`TranslationExtension``{{ t('key') }}`, `{{ tc('key', 'Component') }}` — i18n helpersTwigEngine
----------

[](#twigengine)

```
use Preflow\Twig\TwigEngine;
use Preflow\View\AssetCollector;
use Preflow\View\NonceGenerator;

$assets = new AssetCollector(new NonceGenerator(), isProd: true);

$engine = new TwigEngine(
    templateDirs: [__DIR__ . '/templates', __DIR__ . '/app/pages'],
    assetCollector: $assets,
    debug: false,
    cachePath: __DIR__ . '/storage/twig-cache',  // null = no cache
);

$html = $engine->render('blog/post.twig', ['post' => $post]);
$engine->exists('partials/nav.twig');    // bool
$engine->getTemplateExtension();         // 'twig'
```

Co-located styles and scripts
-----------------------------

[](#co-located-styles-and-scripts)

Use `{% apply css %}` and `{% apply js %}` anywhere in a template. The content is registered with the `AssetCollector` and nothing is output at that point.

```
{# templates/blog/post.twig #}

{% apply css %}
.post-title { font-size: 2rem; font-weight: 700; }
.post-body  { line-height: 1.7; }
{% endapply %}

{% apply js %}
document.querySelector('.post-body a[href^="http"]')
  ?.setAttribute('target', '_blank');
{% endapply %}

{% apply js('head') %}
window.analyticsId = {{ post.id }};
{% endapply %}

{{ post.title }}
{{ post.body|raw }}
```

JS position argument: `'body'` (default), `'head'`, or `'inline'`.

Layout with head() and assets()
-------------------------------

[](#layout-with-head-and-assets)

`{{ head() }}` renders JS registered for the ``. `{{ assets() }}` renders all collected CSS plus body JS — place it just before ``.

```
{# templates/_layout.twig #}

  {% block title %}App{% endblock %}
  {{ head() }}

  {% block content %}{% endblock %}
  {{ assets() }}

```

asset\_url()
------------

[](#asset_url)

The `asset_url()` template function generates a URL to a public asset. Register it via your `AppServiceProvider` to point at the correct public path.

```

```

Register in your service provider:

```
$engine->addFunction(new TemplateFunctionDefinition(
    name: 'asset_url',
    callable: fn (string $path) => '/public/' . ltrim($path, '/'),
    isSafe: false,
));
```

Engine configuration
--------------------

[](#engine-configuration)

Set `APP_ENGINE=twig` in your `.env` (this is the default). Preflow's `Application` will automatically create a `TwigEngine` and register all extension providers.

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance89

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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 ~0 days

Total

18

Last Release

53d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d8b54efbc79683c32645e3fa8d3590037fa003963a16e0ed989104c6f4a2723?d=identicon)[smyr](/maintainers/smyr)

---

Top Contributors

[![smeyerme](https://avatars.githubusercontent.com/u/1925560?v=4)](https://github.com/smeyerme "smeyerme (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.9k](/packages/craftcms-cms)

PHPackages © 2026

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