PHPackages                             slim/twig-view - 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. [Framework](/categories/framework)
4. /
5. slim/twig-view

ActiveLibrary[Framework](/categories/framework)

slim/twig-view
==============

Slim Framework 4 view helper built on top of the Twig 3 templating component

3.4.1(1y ago)3708.0M↓10.1%84[3 issues](https://github.com/slimphp/Twig-View/issues)[4 PRs](https://github.com/slimphp/Twig-View/pulls)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Mar 14Pushed 6mo ago23 watchersCompare

[ Source](https://github.com/slimphp/Twig-View)[ Packagist](https://packagist.org/packages/slim/twig-view)[ Docs](https://www.slimframework.com)[ RSS](/packages/slim-twig-view/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (26)Used By (20)

Slim Framework Twig View
========================

[](#slim-framework-twig-view)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4ee46a5ef4bd3952b4efaa01c8ec151a3c046da1a7e1af7cf2597b17fe8bea08/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f736c696d7068702f747769672d766965772e737667)](https://packagist.org/packages/slim/Twig-View)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)[![Build Status](https://github.com/slimphp/Twig-View/actions/workflows/tests.yml/badge.svg?branch=3.x)](https://github.com/slimphp/Twig-View/actions)[![Coverage Status](https://camo.githubusercontent.com/eba45a7daa356110f05cc23c1b2a6b838adf628531e51eb19ce523834ca2cc79/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f736c696d7068702f547769672d566965772f62616467652e7376673f6272616e63683d332e78)](https://coveralls.io/github/slimphp/Twig-View?branch=3.x)[![Total Downloads](https://camo.githubusercontent.com/da95c3322949a334fc1c99fabd04d49580893db37af412d350c5ca0fe73bcd4e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736c696d2f547769672d566965772e737667)](https://packagist.org/packages/slim/Twig-View/stats)

This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application.

Install
-------

[](#install)

Via [Composer](https://getcomposer.org/)

```
composer require slim/twig-view
```

Requires Slim Framework 4, Twig 3 and PHP 7.4 or newer.

Usage
-----

[](#usage)

### With DI Container

[](#with-di-container)

```
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

require __DIR__ . '/../vendor/autoload.php';

// Create Container
$container = new Container();

// Set view in Container
$container->set(Twig::class, function() {
    return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']);
});

// Create App from container
$app = AppFactory::createFromContainer($container);

// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $container->get(Twig::class)));

// Add other middleware
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

// Render from template file templates/profile.html.twig
$app->get('/hello/{name}', function ($request, $response, $args) {
    $viewData = [
        'name' => $args['name'],
    ];

    $twig = $this->get(Twig::class);

    return $twig->render($response, 'profile.html.twig', $viewData);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $viewData = [
        'name' => $args['name'],
    ];

    $twig = $this->get(Twig::class);
    $str = $twig->fetchFromString('Hi, my name is {{ name }}.', $viewData);
    $response->getBody()->write($str);

    return $response;
});

// Run app
$app->run();
```

### Without DI container

[](#without-di-container)

```
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

require __DIR__ . '/../vendor/autoload.php';

// Create App
$app = AppFactory::create();

// Create Twig
$twig = Twig::create('path/to/templates', ['cache' => 'path/to/cache']);

// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'profile.html.twig', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    $str = $view->fetchFromString(
        'Hi, my name is {{ name }}.',
        [
            'name' => $args['name']
        ]
    );
    $response->getBody()->write($str);

    return $response;
});

// Run app
$app->run();
```

Custom template functions
-------------------------

[](#custom-template-functions)

`TwigExtension` provides these functions to your Twig templates:

- `url_for()` - returns the URL for a given route. e.g.: /hello/world
- `full_url_for()` - returns the URL for a given route. e.g.:
- `is_current_url()` - returns true is the provided route name and parameters are valid for the current path.
- `current_url()` - returns the current path, with or without the query string.
- `get_uri()` - returns the `UriInterface` object from the incoming `ServerRequestInterface` object
- `base_path()` - returns the base path.

You can use `url_for` to generate complete URLs to any Slim application named route and use `is_current_url` to determine if you need to mark a link as active as shown in this example Twig template:

```
User List

    Josh
    Andrew

```

Tests
-----

[](#tests)

To execute the test suite, you'll need to clone the repository and install the dependencies.

```
$ git clone https://github.com/slimphp/Twig-View
$ composer install
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Josh Lockhart](https://github.com/codeguy)
- [Pierre Bérubé](https://github.com/l0gicgate)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance53

Moderate activity, may be stable

Popularity66

Solid adoption and visibility

Community53

Growing community involvement

Maturity78

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

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

Recently: every ~447 days

Total

22

Last Release

197d ago

Major Versions

1.2.0 → 2.02015-08-17

2.5.0 → 3.0.0-alpha2019-04-28

2.x-dev → 3.0.02019-12-02

PHP version history (6 changes)1.0PHP &gt;=5.4.0

2.0PHP &gt;=5.5.0

3.0.0-alphaPHP ^7.1

3.1.0PHP ^7.2

3.2.0PHP ^7.2 || ^8.0

3.3.0PHP ^7.4 || ^8.0

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/921c804872f4924b305115ce412a6f109d341b8ef3d5b905c5969275e295fc70?d=identicon)[codeguy](/maintainers/codeguy)

![](https://avatars.githubusercontent.com/u/33135?v=4)[Rob Allen](/maintainers/akrabat)[@akrabat](https://github.com/akrabat)

![](https://www.gravatar.com/avatar/0a802102aa155a7660b7402a92e3e48e918276c7a16827e0c5de9c466ab4728d?d=identicon)[l0gicgate](/maintainers/l0gicgate)

---

Top Contributors

[![l0gicgate](https://avatars.githubusercontent.com/u/6510935?v=4)](https://github.com/l0gicgate "l0gicgate (151 commits)")[![akrabat](https://avatars.githubusercontent.com/u/33135?v=4)](https://github.com/akrabat "akrabat (57 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (47 commits)")[![adriansuter](https://avatars.githubusercontent.com/u/3974990?v=4)](https://github.com/adriansuter "adriansuter (44 commits)")[![bangzek](https://avatars.githubusercontent.com/u/5100725?v=4)](https://github.com/bangzek "bangzek (36 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (25 commits)")[![silentworks](https://avatars.githubusercontent.com/u/79497?v=4)](https://github.com/silentworks "silentworks (14 commits)")[![odan](https://avatars.githubusercontent.com/u/781074?v=4)](https://github.com/odan "odan (6 commits)")[![nbayramberdiyev](https://avatars.githubusercontent.com/u/23094428?v=4)](https://github.com/nbayramberdiyev "nbayramberdiyev (5 commits)")[![t0mmy742](https://avatars.githubusercontent.com/u/29512640?v=4)](https://github.com/t0mmy742 "t0mmy742 (5 commits)")[![jerfeson](https://avatars.githubusercontent.com/u/2961357?v=4)](https://github.com/jerfeson "jerfeson (5 commits)")[![codeguy](https://avatars.githubusercontent.com/u/31677?v=4)](https://github.com/codeguy "codeguy (3 commits)")[![janjouketjalsma](https://avatars.githubusercontent.com/u/2461961?v=4)](https://github.com/janjouketjalsma "janjouketjalsma (3 commits)")[![GeeH](https://avatars.githubusercontent.com/u/613376?v=4)](https://github.com/GeeH "GeeH (2 commits)")[![twovectors](https://avatars.githubusercontent.com/u/188675?v=4)](https://github.com/twovectors "twovectors (2 commits)")[![trasher](https://avatars.githubusercontent.com/u/224733?v=4)](https://github.com/trasher "trasher (2 commits)")[![llvdl](https://avatars.githubusercontent.com/u/6775525?v=4)](https://github.com/llvdl "llvdl (2 commits)")[![Sam-Burns](https://avatars.githubusercontent.com/u/6594039?v=4)](https://github.com/Sam-Burns "Sam-Burns (2 commits)")[![up9cloud](https://avatars.githubusercontent.com/u/8325632?v=4)](https://github.com/up9cloud "up9cloud (1 commits)")[![Bunkermaster](https://avatars.githubusercontent.com/u/4213013?v=4)](https://github.com/Bunkermaster "Bunkermaster (1 commits)")

---

Tags

phpslim-frameworktwigtwig-templatesframeworktwigslimtemplateview

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[slim/php-view

Render PHP view scripts into a PSR-7 Response object.

2739.7M95](/packages/slim-php-view)[mathmarques/smarty-view

Slim Framework 4 view helper built on top of the Smarty templating component

24134.7k1](/packages/mathmarques-smarty-view)[projek-xyz/slim-plates

Render your Slim 3 application views using Plates template engine.

2678.2k3](/packages/projek-xyz-slim-plates)[kanellov/slim-twig-flash

A Twig extension to access Slim Flash messages in templates

22107.6k9](/packages/kanellov-slim-twig-flash)[rubellum/slim-blade-view

Slim Framework 3 view helper built on the Blade component

1822.4k2](/packages/rubellum-slim-blade-view)

PHPackages © 2026

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