Yii Framework Twig Extension

dev-master / 3.0.x-dev 2020-12-03 08:44 UTC

This package is auto-updated.

Last update: 2020-12-03 08:54:25 UTC



Yii Framework Twig Extension

This extension provides a ViewRender that would allow you to use Twig view template engine with Yii framework.

For license information check the LICENSE-file.

Documentation is at docs/guide/README.md.

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage


The preferred way to install this extension is through composer.

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


You should specify twig and view in the configuration:

return [
    \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',
        ], []));
    WebView::class => static function (Psr\Container\ContainerInterface $container) {
        $webView = new Yiisoft\View\WebView(

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


            'twig' => new \Yiisoft\Yii\Twig\ViewRenderer($container)

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

        return $webView;


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 }}



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

<!DOCTYPE html>
<html lang="">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Yii Demo (Twig)</title>
    {{ this.head()|raw }}
    {{ this.beginBody()|raw }}

        .brandLabel('Yii Demo')
        .options({'class' : 'navbar navbar-light bg-light navbar-expand-sm text-white'})

        .options({'class' : 'navbar-nav mr-auto'})
                {'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')},
        .options({'class' : 'navbar-nav'})
            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')},

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

        <main role="main" class="container py-4">
            {{ content|raw }}
        <footer class="container py-4">
            {{ get('App\\Widget\\PerformanceMetrics').widget()|raw }}

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

Unit testing

The package is tested with PHPUnit. To run tests:


Mutation testing

The package tests are checked with Infection mutation framework. To run it:


Static analysis

The code is statically analyzed with Psalm. To run static analysis:


Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack


The Yii Framework Twig Extension is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.