yii2 twig breadbrumbs不工作

时间:2018-04-11 08:08:53

标签: yii2 twig yii-extensions yii2-basic-app twig-extension

我使用yii作为yii-twig扩展程序的基本应用程序,我想要像yii2默认安装的演示站点一样的面包屑。

我已经做了一些谷歌搜索并尝试了一些事情,但却无法让他们工作。

我的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">
        {% if this.params['breadcrumbs'] %}
        {% set breadcrumbs = this.params['breadcrumbs'] %}
        {% else %}
            {% set breadcrumbs = [] %}
        {% endif %}

        {{ Breadcrumbs.widget({
            'links' : breadcrumbs,
        })|raw(html) }}<!-- breadcrumbs -->

        {{ Alert.widget() }}

        {{ 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。

$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;

1 个答案:

答案 0 :(得分:0)

这可以使用以下代码实现:

{{ use('yii/widgets') }}
{% if this.params['breadcrumbs'] %}
{% set breadcrumbs = this.params['breadcrumbs'] %}
{% else %}
    {% set breadcrumbs = [] %}
{% endif %}

{{ breadcrumbs_widget({
    'links' : breadcrumbs,
}) | raw }}

请注意包含{{ use('yii/widgets') }}{{ breadcrumbs_widget({ ... })

的正确格式

https://www.yiiframework.com/doc/api/2.0/yii-widgets-breadcrumbs