All Projects → yiisoft → view-twig

yiisoft / view-twig

Licence: BSD-3-Clause License
Yii View Twig Renderer

Programming Languages

PHP
23972 projects - #3 most used programming language
Twig
543 projects

Projects that are alternatives of or similar to view-twig

Yii2 Twig
Yii 2 Twig extension.
Stars: ✭ 130 (+441.67%)
Mutual labels:  twig, template-engine
Awesome Twig
A curated list of amazingly awesome Twig extensions, snippets and tutorials
Stars: ✭ 63 (+162.5%)
Mutual labels:  twig, template-engine
Twital
Twital is a "plugin" for Twig that adds some sugar syntax, which makes its templates similar to PHPTal or VueJS.
Stars: ✭ 116 (+383.33%)
Mutual labels:  twig, template-engine
Stick
A golang port of the Twig templating engine
Stars: ✭ 132 (+450%)
Mutual labels:  twig, template-engine
Foil
PHP template engine for native PHP templates
Stars: ✭ 162 (+575%)
Mutual labels:  twig, template-engine
korte
Kotlin cORoutines Template Engine for Multiplatform Kotlin
Stars: ✭ 69 (+187.5%)
Mutual labels:  twig, template-engine
Magento2-Twig
Twig Template Engine for Magento2
Stars: ✭ 58 (+141.67%)
Mutual labels:  twig, template-engine
MulleScion
🌱 A modern template engine for Objective C
Stars: ✭ 14 (-41.67%)
Mutual labels:  twig, template-engine
symfony recipes
Я буду писать здесь рецепты для Symfony на русском
Stars: ✭ 90 (+275%)
Mutual labels:  twig
app-api
API application project template
Stars: ✭ 44 (+83.33%)
Mutual labels:  yii3
yatpl
Yet Another Template Engine 🚀
Stars: ✭ 14 (-41.67%)
Mutual labels:  template-engine
time-ago-bundle
Provides a simple twig filter for expressing time difference in words.
Stars: ✭ 13 (-45.83%)
Mutual labels:  twig
mayflower
Mayflower Mono Repo (react and pattern lab and documentation)
Stars: ✭ 17 (-29.17%)
Mutual labels:  twig
hulk-template
为 CodeIgniter 框架增加视图继承功能,不改变原有视图编写方式,无缝增加视图继承功能。
Stars: ✭ 17 (-29.17%)
Mutual labels:  template-engine
mutex
Mutex lock implementation
Stars: ✭ 28 (+16.67%)
Mutual labels:  yii3
strings
String helper methods and an inflector
Stars: ✭ 31 (+29.17%)
Mutual labels:  yii3
kirby-blade
Enable Laravel Blade Template Engine for Kirby 3
Stars: ✭ 20 (-16.67%)
Mutual labels:  template-engine
Clarkson-Core
A plugin to write Object-Oriented code in combination with the Twig templating engine while keeping the WordPress Way of working in mind.
Stars: ✭ 22 (-8.33%)
Mutual labels:  twig
essential-templating
A set of templating libraries.
Stars: ✭ 21 (-12.5%)
Mutual labels:  template-engine
yii-console
Yii console components
Stars: ✭ 48 (+100%)
Mutual labels:  yii3

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.

Installation

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->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):

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:

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

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

{{ assetManager.register(['App\\Asset\\AppAsset', 'App\\Asset\\CdnFontAwesomeAsset']) }}
{{ this.addCssFiles(assetManager.getCssFiles()) }}
{{ this.addCssStrings(assetManager.getCssStrings()) }}
{{ this.addJsFiles(assetManager.getJsFiles()) }}
{{ this.addJsStrings(assetManager.getJsStrings()) }}
{{ 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:

{{ 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>

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

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

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

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

./vendor/bin/psalm

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].