jakharbek / yii-twig
Yii Framework Twig Extension
Fund package maintenance!
Open Collective
yiisoft
Requires
- twig/twig: ^3.0
- yiisoft/strings: ^3.0@dev
- yiisoft/view: ^3.0@dev
- yiisoft/yii-core: ^3.0@dev
- yiisoft/yii-web: ^3.0@dev
Requires (Dev)
- hiqdev/composer-config-plugin: ^1.0@dev
- phpunit/phpunit: ^7.3
- yiisoft/cache: ^3.0@dev
- yiisoft/di: ^3.0@dev
- yiisoft/log: ^3.0@dev
This package is not auto-updated.
Last update: 2025-03-10 18:13:35 UTC
README
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.
Installation
The preferred way to install this extension is through composer.
php composer.phar require --prefer-dist yiisoft/yii-twig
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
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 templatescontainer
{{ get('App\\Widget\\PerformanceMetrics').widget()|raw }}
Example
main.twig
{{ assetManager.register(['App\\Asset\\AppAsset']) }} {{ this.setCssFiles(assetManager.getCssFiles()) }} {{ this.setJsFiles(assetManager.getJsFiles()) }} {{ this.beginPage()|raw }} <!DOCTYPE html> <html lang=""> <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>Yii Demo (Twig)</title> {{ this.head()|raw }} </head> <body> {{ 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 }} <main role="main" class="container py-4"> {{ content|raw }} </main> <footer class="container py-4"> {{ get('App\\Widget\\PerformanceMetrics').widget()|raw }} </footer> {{ this.endBody()|raw }} </body> </html> {{ this.endPage(true)|raw }}