yiisoft / view-twig
Yii Framework Twig Extension
Fund package maintenance!
Opencollective
yiisoft
Installs: 901
Dependents: 1
Suggesters: 0
Security: 0
Stars: 25
Watchers: 17
Forks: 9
Open Issues: 1
pkg:composer/yiisoft/view-twig
Requires
- php: ^8.0
- psr/container: ^1.0|^2.0
- twig/twig: ^3.3
- yiisoft/view: ^6.0|^7.0|^8.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.4
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.1
- yiisoft/aliases: ^3.0
- yiisoft/psr-dummy-provider: ^1.0
- yiisoft/test-support: ^3.0
This package is auto-updated.
Last update: 2025-10-17 06:01:26 UTC
README
Yii View Twig Renderer
The package is an extension of the Yii View Rendering Library. This extension
provides a TwigTemplateRenderer
that would allow you to use Twig view template engine.
Requirements
- PHP 8.1 - 8.4
Installation
The package could be installed with Composer:
composer require yiisoft/view-twig
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 Twig\Loader\LoaderInterface; use Yiisoft\Aliases\Aliases; use Yiisoft\View\Twig\Extensions\YiiTwigExtension; /** @var array $params */ return [ LoaderInterface::class => static fn (Aliases $aliases) => new FilesystemLoader($aliases->get('@views')) Environment::class => [ '__construct()' => ['options' => $params['yiisoft/view-twig']['options']] ], ];
And also add the parameters for WebView
and Environment
in params.php
:
use Psr\Container\ContainerInterface; use Psr\EventDispatcher\EventDispatcherInterface; use Twig\Environment; use Yiisoft\Aliases\Aliases; use Yiisoft\Definitions\DynamicReference; use Yiisoft\Definitions\Reference; use Yiisoft\View\WebView; use Yiisoft\View\Twig\TwigTemplateRenderer; return [ //... 'yiisoft/view' => [ 'defaultExtension' => 'twig', 'renderers' => [ 'twig' => Reference::to(TwigTemplateRenderer::class), ], ], 'yiisoft/view-twig' => [ 'options' => [ 'cache' => DynamicReference::to(static fn (Aliases $aliases) => $aliases->get('@runtime/twig')), 'charset' => 'utf-8', 'debug' => $_ENV['YII_DEBUG'], //... ], ], //... ];
Template
All variables that were in the regular template are also available in the twig template.
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"> <head> <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 }} </head> <body> {{ this.beginBody()|raw }} <section class="hero is-fullheight is-light"> <div class="hero-head has-background-black"> {{ get('Yiisoft\\Yii\\Bulma\\NavBar').widget() .brandLabel(applicationParameters.getName()) .brandImage('/images/yii-logo.jpg') .options({'class': 'is-black', 'data-sticky': '', 'data-sticky-shadow': ''}) .itemsOptions({'class': 'navbar-end'}) .begin()|raw }} {{ get('Yiisoft\\Yii\\Bulma\\Nav').widget() .currentPath(urlMatcher.getCurrentUri() != null ? urlMatcher.getCurrentUri().getPath() : '') .items([])|raw }} {{ get('Yiisoft\\Yii\\Bulma\\NavBar').end()|raw }} </div> <div class="hero-body is-light"> <div class="container has-text-centered"> {{ content|raw }} </div> </div> <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> <a class="is-hidden-desktop is-size-6" href="https://www.yiiframework.com/" target="_blank" rel="noopener"> {{ applicationParameters.getName() }} </a> </div> <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> </a> </span> <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> </a> </span> <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> </a> </span> <span class="icon"> <a href="https://twitter.com/yiiframework" target="_blank" rel="noopener"> <i class="fab fa-twitter fa-inverse" aria-hidden="true"></i> </a> </span> <span class="icon"> <a href="https://t.me/yii3ru" target="_blank" rel="noopener"> <i class="fab fa-telegram-plane fa-inverse"></i> </a> </span> </div> </div> </div> </section> {{ this.endBody()|raw }} </body> </html> {{ 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. </a> </p>
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii View Twig Renderer is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.