Yii Framework Twig Extension

2.1.0 2023-02-17 15:36 UTC

This package is auto-updated.

Last update: 2024-02-17 13:34:32 UTC


68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667 68747470733a2f2f747769672e73796d666f6e792e636f6d2f696d616765732f747769672d6c6f676f2e706e67

Yii View Twig Renderer

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

The package is an extension of the Yii View Rendering Library. This extension provides a ViewRender that would allow you to use Twig view template engine.


  • PHP 8.0 or higher.


The package could be installed with composer:

composer require yiisoft/view-twig --prefer-dist

General usage

In your application, you should specify the configuration for Twig (by default, this is config/packages/yiisoft/view-twig/common.php):

use Psr\Container\ContainerInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Yiisoft\Aliases\Aliases;
use Yiisoft\View\Twig\Extensions\YiiTwigExtension;
return [
    Environment::class => static function (ContainerInterface $container): Environment {
        $loader = new FilesystemLoader($container

        $twig = new Environment($loader, [
            'cache' => $container
            'charset' => 'utf-8',

        $twig->addExtension(new YiiTwigExtension($container));
        return $twig;

And also override the configuration for WebView (by default, this is config/packages/yiisoft/view/web.php):

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
use Yiisoft\Aliases\Aliases;
use Yiisoft\View\WebView;
use Yiisoft\View\Twig\ViewRenderer;

/** @var array $params */
return [
    WebView::class => static function (ContainerInterface $container) use ($params): WebView {
        $webView = new WebView(

        $webView = $webView
            ->withRenderers(['twig' => new ViewRenderer($container->get(Environment::class))])

        return $webView;


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

The get(string $id); function allows you to get the definition that was set by the container, this function is available in all view templates and layouts:

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

The default main layout of the application template will look like this:

{% do assetManager.register(['App\\Asset\\AppAsset', 'App\\Asset\\CdnFontAwesomeAsset']) %}
{% do this.addCssFiles(assetManager.getCssFiles()) %}
{% do this.addCssStrings(assetManager.getCssStrings()) %}
{% do this.addJsFiles(assetManager.getJsFiles()) %}
{% do this.addJsStrings(assetManager.getJsStrings()) %}
{% do this.addJsVars(assetManager.getJsVars()) %}
{{ this.beginPage()|raw }}
<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{{ this.getTitle() }}</title>
        {{ this.head()|raw }}
    {{ this.beginBody()|raw }}
        <section class="hero is-fullheight is-light">
            <div class="hero-head has-background-black">
                {{ get('Yiisoft\\Yii\\Bulma\\NavBar').widget()
                    .options({'class': 'is-black', 'data-sticky': '', 'data-sticky-shadow': ''})
                    .itemsOptions({'class': 'navbar-end'})
                {{ get('Yiisoft\\Yii\\Bulma\\Nav').widget()
                    .currentPath(urlMatcher.getCurrentUri() != null ? urlMatcher.getCurrentUri().getPath() : '')
                {{ get('Yiisoft\\Yii\\Bulma\\NavBar').end()|raw }}
            <div class="hero-body is-light">
                <div class="container has-text-centered">
                    {{ content|raw }}
            <div class="hero-footer has-background-black">
                <div class="columns is-mobile">
                    <div class="column has-text-left has-text-light">
                        <i class="fas fa-copyright fa-inverse is-hidden-mobile"></i>
                        <a class="is-hidden-mobile" href="https://www.yiiframework.com/" target="_blank" rel="noopener">
                            {{ 'now'|date('Y') }} {{ applicationParameters.getName() }}
                        <a class="is-hidden-desktop is-size-6" href="https://www.yiiframework.com/" target="_blank" rel="noopener">
                            {{ applicationParameters.getName() }}
                    <div class="column has-text-centered has-text-light is-hidden-mobile"></div>
                    <div class="column has-text-right has-text-light">
                        <span class="icon">
                            <a href="https://github.com/yiisoft" target="_blank" rel="noopener">
                                <i class="fab fa-github fa-inverse" aria-hidden="true"></i>
                        <span class="icon">
                            <a href="https://join.slack.com/t/yii/shared_invite/enQtMzQ4MDExMDcyNTk2LTc0NDQ2ZTZhNjkzZDgwYjE4YjZlNGQxZjFmZDBjZTU3NjViMDE4ZTMxNDRkZjVlNmM1ZTA1ODVmZGUwY2U3NDA" target="_blank" rel="noopener">
                                <i class="fab fa-slack fa-inverse " aria-hidden="true"></i>
                        <span class="icon">
                            <a href="https://www.facebook.com/groups/yiitalk" target="_blank" rel="noopener">
                                <i class="fab fa-facebook-f fa-inverse" aria-hidden="true"></i>
                        <span class="icon">
                            <a href="https://twitter.com/yiiframework" target="_blank" rel="noopener">
                                <i class="fab fa-twitter fa-inverse" aria-hidden="true"></i>
                        <span class="icon">
                            <a href="https://t.me/yii3ru" target="_blank" rel="noopener">
                                <i class="fab fa-telegram-plane fa-inverse"></i>
    {{ this.endBody()|raw }}
{{ this.endPage(true)|raw }}

And the view template of the main page (site/index) will be as follows:

{% do this.setTitle(applicationParameters.getName()) %}

<h1 class="title">Hello!</h1>

<p class="subtitle">Let's start something great with <strong>Yii3</strong>!</p>

<p class="subtitle is-italic">
    <a href="https://github.com/yiisoft/docs/tree/master/guide/en" target="_blank" rel="noopener">
        Don't forget to check the guide.


Unit testing

The package is tested with PHPUnit. To run tests:


Mutation testing

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


Static analysis

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



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.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack