Php 树枝在Yii2中不工作

Php 树枝在Yii2中不工作,php,yii2,twig,yii2-advanced-app,yii-extensions,Php,Yii2,Twig,Yii2 Advanced App,Yii Extensions,我想在Yii2框架中使用Twig,但它不起作用 我正在使用作为基础项目,但我是Yii世界的新手,因此我认为我没有以正确的方式配置Twig 首先,我使用以下方式下载: composer require yiisoft/yii2-twig 然后我按照以下说明进行操作,但不容易理解: 它说: 要开始使用Twig,您需要如下配置视图组件: [ 'components' => [ 'view' => [ 'class' => 'yii\

我想在Yii2框架中使用Twig,但它不起作用

我正在使用作为基础项目,但我是Yii世界的新手,因此我认为我没有以正确的方式配置Twig

首先,我使用以下方式下载:

composer require yiisoft/yii2-twig
然后我按照以下说明进行操作,但不容易理解:

它说: 要开始使用Twig,您需要如下配置视图组件:

[
    'components' => [
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        'html' => ['class' => '\yii\helpers\Html'],
                    ],
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
]
我必须将此代码粘贴到哪个文件中?


在index.php文件中,我添加了以下代码,但不起作用:

{% if true %}
    <p>It is true.</p>
{% else %}
    <p>It is false.</p>        
{% endif %}
{%if true%}
这是真的

{%else%} 这是假的。

{%endif%}
我是这样解决的:

我修改了backend/config/main本地php文件:

<?php

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'CPeTotdTU98geIyM7q0PljmCpJbupPN4',
        ],
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'globals' => [
                        'html' => ['class' => '\yii\helpers\Html'],
                    ],
                    'uses' => ['yii\bootstrap'],
                ],
                // ...
            ],
        ],
    ],
];

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;
然后我将backend/views/sire/index.php文件的名称修改为index.twig

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