简体   繁体   English

将HTML Megamenu转换为Yii CMenu zii Widget

[英]Convert HTML Megamenu to Yii CMenu zii Widget

I just started working with Yii, and I am having some trouble converting my HTML Megamenu to Yii. 我刚开始使用Yii,但在将HTML Megamenu转换为Yii时遇到了一些麻烦。 Basically my html is something like this: 基本上我的html是这样的:

<div class="nav-wrapper">
    <div class="container">
        <ul class="some_class">
            <li class="active"><a href="#">Parent 1</a>
                <div class="megamenu">
                    <div class="row">
                        <a href="#" class="overview">Child 1</a>
                    </div>
                    <div class="row">
                        <div class="col1">
                            <ul>
                                <li><a href="#">Child 3</a></li>
                                <li><a href="#">Child 4</a></li>
                            </ul>
                        </div>              
                    </div>
                </div>
            </li>
         </ul>
     </div>

Adapting this to CMenu widget is proving more difficult than I thouht...especially for e starter like me. 事实证明,要使它适应CMenu小部件比我要困难得多,尤其是对于像我这样的初学者。 I can come up with the classes and lists, but how do I put the Divs within the CMenu Widget? 我可以提出类和列表,但是如何将Divs放在CMenu小部件中?

Thanks 谢谢

You should create your own widget. 您应该创建自己的小部件。 Creating widget is not harder than creating controllers. 创建小部件并不比创建控制器难。

Start from something simple, to see how it plays. 从简单的东西开始,看看它如何发挥作用。 Lets try something, this is quick example, does not handle recursion, but could be fine: 让我们尝试一下,这是一个简单的示例,它不处理递归,但是可以:

class MyMenu extends CWidget
{
    public $items = [];
    public function init()
    {
       // Possibli do something with items
    }

    public function run()
    {
        $this->render('menu', ['items' => $this->items]);
    }
}

In views/menu.php: 在views / menu.php中:

<div class="nav-wrapper">
    <div class="container">
        <ul class="some_class">
              <?php foreach($items as $item):?>
            <li class="active"><a href="#"><?=$item['title'];?></a>
                <div class="megamenu">

                    <div class="row">
                        <a href="#" class="overview"><?= $item['overview'];?></a>
                    </div>
                    <div class="row">
                        <div class="col1">
                            <ul>
                                         <?php foreach($item['items'] as $row):?>
                                <li><a href="#"><?= $row['title'];?></a></li>
                                          <?php endforeach;?>
                            </ul>
                        </div>
                    </div>
                </div>
            </li>
                <?php endforeach;?>
         </ul>
     </div>
</div>

Then use it in your ccontorller view 然后在您的ccontorller视图中使用它

<?php
$this->widget('path.to.MyMenu', [
    'items' => [
        [
            'title' => 'Foo',
            'overview' => 'Some overview',
            'items' => [
                [
                    'title' => 'Bar'
                ],
                [
                    'title' => 'Baz'
                ]
            ]
        ]
    ]
]);
?>

DISCLAIMER : Not tested, just to show idea, might work or not. 免责声明 :未经测试,只是为了展示想法,可能有效或无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM