首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2 Twig不适用于页面查看

Yii2 Twig不适用于页面查看
EN

Stack Overflow用户
提问于 2018-04-11 05:43:25
回答 1查看 290关注 0票数 1

我安装了yii2-basic-app并安装了小枝扩展,并制作了一个工作良好的main.twi布局。

然而,我的页面视图在站点文件夹中仍然具有.php扩展名,并且也是php文件。

如果我将这些名称重命名为具有.twig扩展并放入小枝代码,则中断(因为它仍在为主页查找index.php ),如果将.twig重命名为.php,则会将树枝代码输出到页面中。

我怎样才能像对main.twig布局那样使用小枝来查看页面呢?

自从我发布了这个问题之后,我做了更多的googling搜索,并在堆栈溢出上找到了一个类似的问题,这个人修改了SiteController并在这里添加了.twig扩展,但是我也读到在web.php配置中定义扩展是可能的,但是我一直找不到如何做到这一点。

我的main.twig的内容是:

代码语言:javascript
复制
{{ use('app/assets/AppAsset') }}
{{ register_app_asset() }}

{{ this.beginPage() }}
<!DOCTYPE html>
<html lang="{{app.language}}">
<head>
    <meta charset="{{app.charset}}"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">      
    {{ html.csrfMetaTags() | raw }}
    <title>{{ html.encode(this.title) }}</title>
    {{ this.head() }}
</head>
<body>
{{ this.beginBody() }}

<div class="wrap">
    {{ use('yii/bootstrap') }}
    {{ nav_bar_begin({
        'brandLabel': app.name,
        'brandUrl': app.homeUrl,
        'options': [{
            'class': 'navbar-inverse navbar-fixed-top',
        }],
    }) }}
    {% set menuItems = [] %}
    {% set menuItems = menuItems|merge([
        {'label': 'Home', 'url': ['/site/index']},
        {'label': 'About', 'url': ['/site/about']},
        {'label': 'Contact', 'url': ['/site/contact']},
    ])
    %}
    {% if app.user.isGuest == false %}
        {% set menuItems = menuItems|merge([
            {
                'label' : 'logout (' ~ app.user.identity.username ~ ')',
                'url' : ['/site/logout'],
                'linkOptions' : {'data-method' : 'post'}
            }
        ])
    %}
    {% else %}
        {% set menuItems = menuItems|merge([
            {'label' : 'login', 'url' : ['/site/login']},
        ])
    %}
    {% endif %}
    {{ nav_widget({
        'options': {
            'class': 'navbar-nav navbar-right',
        },
        'items': menuItems
    }) }}
    {{ nav_bar_end() }}

    <div class="container">
        {{ content | raw }}
    </div>
</div>

<footer class="footer">
    <div class="container">
        <p class="pull-left">&copy; My Company {{ 'now'|date('Y') }}</p>
        <p class="pull-right">{{ Yii.powered() | raw }}</p>
    </div>
</footer>

{{ this.endBody() }}
</body>
</html>
{{ this.endPage() }}

这也是我的config/web.php文件,它是如何设置yii和twig的。

代码语言:javascript
复制
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'ywLqCkq0cMC-cvfVXiGXFnfu2S41_CbC',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            //'class' => 'yii\web\UrlManager',
            'baseUrl' => '/',
            'rules' => [
                '/' => 'site/index',
                'about' => 'site/about',
                'contact' => 'site/contact',
                'login' => 'site/login',
            ],
        ],
        /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
        // setting up twig
        'view' => [
            'class' => 'yii\web\View',
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => false, // '@runtime/Twig/cache',
                    // Array of twig options:
                    'options' => [
                        'auto_reload' => true,
                        YII_DEBUG ? [ 'debug' => true, ] : [],
                    ],
                    'extensions' => YII_DEBUG ? [ '\Twig_Extension_Debug', ] : [],
                    'globals' => [
                        'html' => '\yii\helpers\Html',
                        'url' => '\yii\helpers\Url',
                        'yii' => 'Yii',
                    ],
                    'uses' => ['yii\bootstrap'],
                ],
            ],
        ],
    ],
    'layout' => 'main.twig',
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
    ];
}

return $config;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-11 07:15:32

代码语言:javascript
复制
    // setting up twig
    'view' => [
        'class' => 'yii\web\View',
        'defaultExtension' => 'twig',
        // ...
    ],

https://www.yiiframework.com/doc/api/2.0/yii-base-view#$defaultExtension-detail

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49767350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档