PHP code example of yiisoft / view-twig

1. Go to this page and download the library: Download yiisoft/view-twig library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yiisoft / view-twig example snippets


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;
    },
];

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;
    },
    //...
];