Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Twig template engine with Yii2 framework?

Tags:

php

twig

yii2

I followed the instructions on yii2 documentation about using twig template engine

in config/web.php (which is included from index.php) I have:

'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    'options' => ['auto_reload' => true], /*  Array of twig options */
                    'globals' => ['html' => '\yii\helpers\Html'],
                ],
            ],
        ],

in SiteController.php:

public function actionIndex()
    {
        echo $this->render('index.twig');
    }

in views/site/index.twig I have some text:

But instead of seeing raw html I see template based on views/layouts/main.php with index.twig content used as variable in main layout.

like image 786
Sergey P. aka azure Avatar asked Jan 25 '26 01:01

Sergey P. aka azure


1 Answers

It was required to set layout to false in order to skip layout processing during redner

class BaseController extends Controller
{
    public $layout = false;
}
like image 63
Sergey P. aka azure Avatar answered Jan 26 '26 16:01

Sergey P. aka azure