https://github.com/yiisoft/view-twig
Yii View Twig Renderer
https://github.com/yiisoft/view-twig
hacktoberfest template-engine twig yii3
Last synced: 5 months ago
JSON representation
Yii View Twig Renderer
- Host: GitHub
- URL: https://github.com/yiisoft/view-twig
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2018-08-02T16:03:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-12-06T22:18:04.000Z (6 months ago)
- Last Synced: 2025-12-27T06:53:26.517Z (6 months ago)
- Topics: hacktoberfest, template-engine, twig, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 193 KB
- Stars: 26
- Watchers: 16
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii View Twig Renderer
[](https://packagist.org/packages/yiisoft/view-twig)
[](https://packagist.org/packages/yiisoft/view-twig)
[](https://github.com/yiisoft/view-twig/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/view-twig)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/view-twig/master)
[](https://github.com/yiisoft/view-twig/actions/workflows/static.yml?query=branch%3Amaster)
[](https://shepherd.dev/github/yiisoft/view-twig)
The package is an extension of the [Yii View Rendering Library](https://github.com/yiisoft/view/). This extension
provides a `TwigTemplateRenderer` that would allow you to use [Twig](https://twig.symfony.com/) view template engine.
## Requirements
- PHP 8.1 - 8.5
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
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`):
```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`:
```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](https://github.com/yiisoft/app) will look like this:
```twig
{% 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 }}
{{ this.getTitle() }}
{{ this.head()|raw }}
{{ this.beginBody()|raw }}
{{ 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 }}
{{ content|raw }}
{{ this.endBody()|raw }}
{{ this.endPage(true)|raw }}
```
And the view template of the main page (`site/index`) will be as follows:
```twig
{% do this.setTitle(applicationParameters.getName()) %}
Hello!
Let's start something great with Yii3!
Don't forget to check the guide.
```
## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
## License
The Yii View Twig Renderer is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.
Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)