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

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

tamdaz/tempest-twig
===================

A third-party package that provides Twig for Tempest.

v0.1.0(1mo ago)00[1 PRs](https://github.com/tamdaz/tempest-twig/pulls)MITPHPPHP ^8.5CI passing

Since Mar 28Pushed 1mo agoCompare

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

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

tempest-twig
============

[](#tempest-twig)

Tempest Twig is a third-party package that integrates the Twig templating engine with the [Tempest framework](https://tempestphp.com). It provides full Twig `3.x` support, custom extensions for routing and debugging, and an innovative component system using HTML-like syntax.

The package includes flexible configuration for template paths, built-in Twig extensions that work with Tempest's routing and Vite pipeline, and a sophisticated component transformation system. Everything is fully tested with PHPUnit and includes GitHub Actions CI.

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

[](#installation)

First, install the Composer package:

```
composer require tamdaz/tempest-twig
```

Configuration
-------------

[](#configuration)

To use Tempest Twig, create a configuration file in your `config` directory. Tempest will automatically discover and load this configuration.

```
// config/twig.config.php
use Tamdaz\TempestTwig\TwigConfig;
use function Tempest\root_path;

return new TwigConfig(
    viewPaths: [
        root_path('templates'),
    ],
    debug: env('APP_DEBUG', false),
    strictVariables: true,
);
```

The `TwigConfig` class wraps Twig's standard options. The `viewPaths` parameter specifies which directories to search for templates. You can add multiple directories for different template locations. Other options like `debug`, `charset`, `strictVariables`, and `autoescape` follow Twig's standard behavior.

Twig Functions
--------------

[](#twig-functions)

Tempest Twig automatically registers custom Twig functions that integrate with the Tempest framework. These functions are available in all templates without any additional setup.

### Routing Functions

[](#routing-functions)

The `route()` function generates URLs for named routes or controller methods. You can pass route parameters directly:

```
{{ route('route.name') }}
{{ route([ControllerClass::class, 'method']) }}
{{ route([ControllerClass::class, 'method'], param1, param2) }}
```

Use `signed_route()` to generate URLs with cryptographic signatures. Use `temporary_signed_route()` for links that expire after a duration (in seconds):

```
{{ signed_route('route.name') }}
{{ temporary_signed_route('route.name', 3600) }}
```

Check the current route with `is_current_route()` to highlight active navigation items:

```
{% if is_current_route('route.name') %}
  Active
{% endif %}
```

The `current_path` variable provides the current request path as a string.

### Vite Functions

[](#vite-functions)

If your Tempest application uses Vite for asset bundling, the package provides integration functions. Use `vite_tags()` to generate script and link tags for Vite entry points:

```
{{ vite_tags('resources/js/app.ts', 'resources/css/app.css') }}
```

Use `vite_asset()` to get the public URL of an asset from Vite's manifest:

```

```

### Debug Functions

[](#debug-functions)

Tempest Twig includes debugging and utility functions. The `dump()` function inspects variables during development. The `class()` function returns an object's class name, and `is_empty()` checks if a variable is empty:

```
{{ dump(user, post) }}
{{ class(user) }}
{% if is_empty(posts) %}No posts{% endif %}
```

Additional utilities include `get_type()` for variable types, `env()` for environment variables with fallback, and `to_json()` for converting PHP values to JSON:

```
{{ get_type(value) }}
{{ env('APP_NAME', 'MyApp') }}

  const data = {{ to_json(users) }};

```

The `count()` function counts array elements, `current_url` gives the full URL, and `now` provides the current timestamp.

Component System
----------------

[](#component-system)

Tempest Twig includes a component system using HTML-like syntax. Instead of writing Twig `include` and `embed` directives, you write component tags that look like HTML custom elements. The package transforms these tags into native Twig directives automatically.

### Self-Closing Components

[](#self-closing-components)

Self-closing components are ideal for simple UI elements like buttons and badges. Write them as XML-style tags with attributes:

```

```

This transforms into a Twig `include` directive that passes attributes as variables:

```
{% include 'components/Button.html.twig' with { label: 'Click me', variant: 'primary' } only %}
```

Your component template at `templates/components/Button.html.twig`:

```

  {{ label }}

```

### Components with Content

[](#components-with-content)

Components can wrap content, similar to Vue or React components. When you add content between tags, it transforms into an `embed` directive:

```

  This is a warning message

```

This becomes:

```
{% embed 'components/Alert.html.twig' with { type: 'warning' } %}
  {% block content %}This is a warning message{% endblock %}
{% endembed %}
```

Your component template accesses the content through Twig's block system:

```

  {% block content %}{% endblock %}

```

### Components with Named Slots

[](#components-with-named-slots)

For complex components, define named content areas using `` tags. This allows different parts to accept different content:

```

    {{ title }}

  Card content goes here

```

Your component template can define multiple named blocks:

```

  {% if block('header') is not empty %}

      {% block header %}{% endblock %}

  {% endif %}

    {% block content %}{% endblock %}

```

Default content (anything not in ``) goes into the `content` block. Named blocks are optional, so check if they have content before rendering them.

### Attribute Binding

[](#attribute-binding)

Components accept both static attributes and dynamic Twig expressions. Static attributes are quoted strings, while dynamic attributes use a colon prefix:

```

```

All attributes are available as variables in your component. The preprocessor handles nested components and escapes attribute values properly.

### Component Organization

[](#component-organization)

Organize components in a dedicated `components` directory within your template's folder. This separates them from page templates and layouts:

```
templates/
├── components/
│   ├── Button.html.twig
│   ├── Card.html.twig
│   ├── Alert.html.twig
│   └── UserProfile.html.twig
├── layouts/
│   └── base.html.twig
└── pages/
    ├── home.html.twig
    └── about.html.twig

```

Always provide a default `content` block for the main content area. For optional slots, check if the block is empty before rendering to avoid extra HTML. Use PascalCase for component names to distinguish them from standard HTML tags.

How It Works
------------

[](#how-it-works)

Tempest Twig integrates several components. The `TwigInitializer` is the entry point discovered by Tempest's service container. It registers the Twig environment, sets up the `ComponentLoader` for transformation, and registers custom extensions. The `TwigViewRendererInitializer` then selects the `TwigViewRenderer` as the default renderer.

The `ComponentPreprocessor` transforms your component syntax into standard Twig directives before Twig processes the template. Component tags become `include` or `embed` directives with proper variable passing. The transformation is transparent, so you never think about the underlying Twig code.

Three extensions are automatically registered. The `DebugExtension` provides debugging utilities. The `RoutingExtension` integrates with Tempest's routing system. The `ViteExtension` handles Vite asset manifest integration.

Testing and Development
-----------------------

[](#testing-and-development)

The package includes a test suite covering component attribute parsing, template transformation, and loader functionality. Run tests with:

```
composer test
```

All tests use PHPUnit and follow the standard test directory structure. The package includes GitHub Actions workflows for continuous integration and compatibility checks.

License
-------

[](#license)

This package is licensed under the MIT license. See `LICENSE` for details.

Support and Contributing
------------------------

[](#support-and-contributing)

If you encounter any issues or have suggestions for improvements, you can open an issue or a PR on GitHub. Contributions are welcome!

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

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

46d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ff91284d6b91c6b9335bb1cbdfd49c2069d91f065efb1b7c5ef40a18bb0a7c8?d=identicon)[tamdaz](/maintainers/tamdaz)

---

Top Contributors

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

---

Tags

tempestthird-partytwig

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

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