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

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

jakharbek/yii-twig
==================

Yii Framework Twig Extension

01PHPCI failing

Since May 10Pushed 6y ago1 watchersCompare

[ Source](https://github.com/jakharbek/yiisoft-twig)[ Packagist](https://packagist.org/packages/jakharbek/yii-twig)[ RSS](/packages/jakharbek-yii-twig/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

 [ ![](https://camo.githubusercontent.com/baea95c9cb6fb1dea300746b392f38db3338f9bc0a5eae0d4a4e06066fd5b66a/68747470733a2f2f747769672e73796d666f6e792e636f6d2f696d616765732f747769672d6c6f676f2e706e67) ](https://twig.symfony.com/)

Yii Framework Twig Extension
============================

[](#yii-framework-twig-extension)

This extension provides a `ViewRender` that would allow you to use [Twig](http://twig.sensiolabs.org/) view template engine with [Yii framework](http://www.yiiframework.com).

For license information check the [LICENSE](LICENSE.md)-file.

Documentation is at [docs/guide/README.md](docs/guide/README.md).

[![Latest Stable Version](https://camo.githubusercontent.com/5feac50a626ace5f3d66769c2511705be303a1846825f873e7b36c0af99f1f3f/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d747769672f762f737461626c652e706e67)](https://packagist.org/packages/yiisoft/yii-twig)[![Total Downloads](https://camo.githubusercontent.com/ef6aa7c4827a55a2522b31a191fcd7fd2bd2945f3bfef7624fee4fb71c9cf240/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d747769672f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yiisoft/yii-twig)[![Build Status](https://camo.githubusercontent.com/b6f2ded0e8ab476564784bef3ad308965d09cbcd3970a45f199927dbdc47e281/68747470733a2f2f7472617669732d63692e6f72672f796969736f66742f7969692d747769672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yiisoft/yii-twig)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

```
php composer.phar require --prefer-dist yiisoft/yii-twig

```

Usage
-----

[](#usage)

You should specify `twig` and `view` in the configuration:

```
//Twig
return [
    //...
    //Twig
    \Twig\Environment::class => static function (Psr\Container\ContainerInterface $container) {
        $loader = new \Twig\Loader\FilesystemLoader($container->get(Yiisoft\Aliases\Aliases::class)->get('@views'));

        return new \Twig\Environment($loader, array_merge([
            'cache' =>$container->get(Yiisoft\Aliases\Aliases::class)->get('@runtime/cache/twig'),
            'charset' => 'utf-8',
        ], []));
    },
    //View
    WebView::class => static function (Psr\Container\ContainerInterface $container) {
        $webView = new Yiisoft\View\WebView(
            $container->get(Yiisoft\Aliases\Aliases::class)->get('@views'),
            $container->get(Yiisoft\View\Theme::class),
            $container->get(Psr\EventDispatcher\EventDispatcherInterface::class),
            $container->get(\Psr\Log\LoggerInterface::class)
        );

        $webView->setDefaultParameters(
            [
                'assetManager' => $container->get(Yiisoft\Assets\AssetManager::class),
                'urlGenerator' => $container->get(Yiisoft\Router\UrlGeneratorInterface::class),
            ]
        );

        $webView->setDefaultExtension('twig');

        $webView->setRenderers([
            'twig' => new \Yiisoft\Yii\Twig\ViewRenderer($container)
        ]);

        $container->get(\Twig\Environment::class)->addExtension(new \Yiisoft\Yii\Twig\Extensions\Yii_Twig_Extension($container));

        return $webView;
    },
    //...
]
```

Template
--------

[](#template)

All variables that were in the regular template are also available in the twig template.

- `get(string id)` this is a function of accessing the container, in addition, there is a global variable throughout the templates `container`

```
{{ get('App\\Widget\\PerformanceMetrics').widget()|raw }}

```

Example
-------

[](#example)

main.twig

```
{{ assetManager.register(['App\\Asset\\AppAsset']) }}
{{ this.setCssFiles(assetManager.getCssFiles()) }}
{{ this.setJsFiles(assetManager.getJsFiles()) }}
{{ this.beginPage()|raw }}

    Yii Demo (Twig)
    {{ this.head()|raw }}

    {{ this.beginBody()|raw }}

        {{
        get('Yiisoft\\Yii\\Bootstrap4\\NavBar').begin()
        .brandLabel('Yii Demo')
        .brandUrl(urlGenerator.generate('site/index'))
        .options({'class' : 'navbar navbar-light bg-light navbar-expand-sm text-white'})
        .start()|raw
        }}

        {{
        get('Yiisoft\\Yii\\Bootstrap4\\Nav').widget()
        .currentPath(currentUrl)
        .options({'class' : 'navbar-nav mr-auto'})
        .items(
            [
                {'label' : 'Blog', 'url' : urlGenerator.generate('blog/index')},
                {'label' : 'Comments Feed', 'url' : urlGenerator.generate('blog/comment/index')},
                {'label' : 'Users', 'url' : urlGenerator.generate('user/index')},
                {'label' : 'Contact', 'url' : urlGenerator.generate('site/contact')},
            ]
        )|raw
        }}

        {{
        get('Yiisoft\\Yii\\Bootstrap4\\Nav').widget()
        .currentPath(currentUrl)
        .options({'class' : 'navbar-nav'})
        .items(
            user.getId() == null ?
            [
                {'label' : 'Login', 'url' : urlGenerator.generate('site/login')},
                {'label' : 'Signup', 'url' : urlGenerator.generate('site/signup')},
            ]
            :
            [
                {'label' : "Logout (" ~ user.getLogin() ~ ")", 'url' : urlGenerator.generate('site/logout')},
            ]
        )|raw
        }}

        {{ get('Yiisoft\\Yii\\Bootstrap4\\NavBar').end()|raw }}

            {{ content|raw }}

            {{ get('App\\Widget\\PerformanceMetrics').widget()|raw }}

    {{ this.endBody()|raw }}

{{ this.endPage(true)|raw }}
```

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

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