yii 基本应用程序,yii-twig classes 在使用 nav_bar_begin 时设置为 0-class
yii basic app, yii-twig classes being set with 0-class when using nav_bar_begin
我将 yii 用作带有 yii-twig 扩展的基本应用程序,并使用 nav_bar_begin 创建导航菜单,但 class 的设置与我预期的不同,因此导航菜单是灰色的,而不是应该的黑色。
当我查看源代码时,我看到了这个:
<nav id="w0" class="navbar navbar-default" 0-class="navbar-inverse navbar-fixed-top">
<div class="container"><div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w0-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">My Application</a>
</div>
<div id="w0-collapse" class="collapse navbar-collapse">
<ul id="w1" class="navbar-nav navbar-right nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/login">login</a></li>
</ul>
</div>
</div>
没有为我的样式表正确设置的部分是第一行,它有 class="..."
和 0-class="..."
.
0-class
中的样式实际上应该在 class
中。
我的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;
您的微件配置有误。替换
'options': [{
'class': 'navbar-inverse navbar-fixed-top',
}],
和
'options': {
'class': 'navbar-inverse navbar-fixed-top',
},
我将 yii 用作带有 yii-twig 扩展的基本应用程序,并使用 nav_bar_begin 创建导航菜单,但 class 的设置与我预期的不同,因此导航菜单是灰色的,而不是应该的黑色。
当我查看源代码时,我看到了这个:
<nav id="w0" class="navbar navbar-default" 0-class="navbar-inverse navbar-fixed-top">
<div class="container"><div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w0-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">My Application</a>
</div>
<div id="w0-collapse" class="collapse navbar-collapse">
<ul id="w1" class="navbar-nav navbar-right nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/login">login</a></li>
</ul>
</div>
</div>
没有为我的样式表正确设置的部分是第一行,它有 class="..."
和 0-class="..."
.
0-class
中的样式实际上应该在 class
中。
我的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;
您的微件配置有误。替换
'options': [{
'class': 'navbar-inverse navbar-fixed-top',
}],
和
'options': {
'class': 'navbar-inverse navbar-fixed-top',
},