Yii2 Twig 不适用于页面浏览量
Yii2 Twig not working for page views
我已经安装了 yii2-basic-app 和 twig 扩展,并制作了一个运行良好的 main.twi 布局。
不过,我在站点文件夹中的页面浏览量仍然具有 .php 扩展名,并且也是 php 文件。
如果我将它们重命名为具有 .twig 扩展名并放入 twig 代码,那么如果我将 .twig 重命名回 .php 它将树枝代码吐出到页面中。
如何像使用 main.twig 布局一样使用 twig 来提高页面浏览量?
自从发布问题后,我又进行了一些谷歌搜索,并在 Whosebug 上发现了一个类似的问题,此人修改了 SiteController 并在此处添加了 .twig 扩展名,但我还了解到可以定义扩展名在 web.php 配置中,但我一直无法找到如何执行此操作。
我的main.twig的内容是:
{{ 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">© 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 的文件。
$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;
// setting up twig
'view' => [
'class' => 'yii\web\View',
'defaultExtension' => 'twig',
// ...
],
https://www.yiiframework.com/doc/api/2.0/yii-base-view#$defaultExtension-detail
我已经安装了 yii2-basic-app 和 twig 扩展,并制作了一个运行良好的 main.twi 布局。
不过,我在站点文件夹中的页面浏览量仍然具有 .php 扩展名,并且也是 php 文件。
如果我将它们重命名为具有 .twig 扩展名并放入 twig 代码,那么如果我将 .twig 重命名回 .php 它将树枝代码吐出到页面中。
如何像使用 main.twig 布局一样使用 twig 来提高页面浏览量?
自从发布问题后,我又进行了一些谷歌搜索,并在 Whosebug 上发现了一个类似的问题,此人修改了 SiteController 并在此处添加了 .twig 扩展名,但我还了解到可以定义扩展名在 web.php 配置中,但我一直无法找到如何执行此操作。
我的main.twig的内容是:
{{ 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">© 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 的文件。
$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;
// setting up twig
'view' => [
'class' => 'yii\web\View',
'defaultExtension' => 'twig',
// ...
],
https://www.yiiframework.com/doc/api/2.0/yii-base-view#$defaultExtension-detail