Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yiisoft/view-twig
Yii View Twig Renderer
https://github.com/yiisoft/view-twig
hacktoberfest template-engine twig yii3
Last synced: 4 days 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 (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-10T14:07:36.000Z (3 months ago)
- Last Synced: 2024-10-29T14:35:37.931Z (2 months ago)
- Topics: hacktoberfest, template-engine, twig, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 188 KB
- Stars: 25
- Watchers: 20
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- 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
[![Latest Stable Version](https://poser.pugx.org/yiisoft/view-twig/v)](https://packagist.org/packages/yiisoft/view-twig)
[![Total Downloads](https://poser.pugx.org/yiisoft/view-twig/downloads)](https://packagist.org/packages/yiisoft/view-twig)
[![Build status](https://github.com/yiisoft/view-twig/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/view-twig/actions/workflows/build.yml)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiisoft/view-twig/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/view-twig/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/yiisoft/view-twig/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/view-twig/?branch=master)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fview-twig%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/view-twig/master)
[![static analysis](https://github.com/yiisoft/view-twig/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/view-twig/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/view-twig/coverage.svg)](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 `ViewRender` that would allow you to use [Twig](https://twig.symfony.com/) view template engine.## Requirements
- PHP 8.0 or higher.
## 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 Yiisoft\Aliases\Aliases;
use Yiisoft\View\Twig\Extensions\YiiTwigExtension;
return [
Environment::class => static function (ContainerInterface $container): Environment {
$loader = new FilesystemLoader($container
->get(Aliases::class)
->get('@views'));$twig = new Environment($loader, [
'cache' => $container
->get(Aliases::class)
->get('@runtime/cache/twig'),
'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`):
```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(
$container
->get(Aliases::class)
->get('@views'),
$container->get(EventDispatcherInterface::class),
);$webView = $webView
->withDefaultExtension('twig')
->withRenderers(['twig' => new ViewRenderer($container->get(Environment::class))])
;$webView->setCommonParameters($params['yiisoft/view']['commonParameters']);
return $webView;
},
//...
];
```### Template
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:```twig
{{ get('App\\Widget\\PerformanceMetrics').widget()|raw }}
```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
[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective&logoColor=7eadf1&labelColor=555555)](https://opencollective.com/yiisoft)
## Follow updates
[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)
[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter&logoColor=1DA1F2&labelColor=555555?style=flat)](https://twitter.com/yiiframework)
[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat&logo=telegram)](https://t.me/yii3en)
[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat&logo=facebook&logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)
[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat&logo=slack)](https://yiiframework.com/go/slack)