如何优化菜单导航

发布于 2024-12-20 02:26:51 字数 1677 浏览 0 评论 0原文

我正在构建一个菜单选项,在最后一个选项中出现问题,锚点方法不能作为弹出新窗口的链接。此外,在选项1和2中,我重复了那些看起来不太好的代码。

有没有更好的方法来优化这些代码?让它更干净。

在我的控制器中:

public function loadPage($name, $pageID) {
    $data['title'] = $this->tabPageData;
    $data['tabMenu'] = $this->model->getAllMenuItems(); 

    if ($name == 'portfolio-1') {
        // load portfolio 1, get the page content (photos) and its name
        $data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
        $data['pageName'] = $this->model->getPageNameByID($pageID);
    } elseif ($name == 'portfolio-2') {
        $data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
        $data['pageName'] = $this->model->getPageNameByID($pageID);
    } elseif ($name == 'contact') {
        // load Contact page
        $data['tabContact'] = $this->model->getContactByPageID($pageID);
    } else {
        // load a Blog site
        echo anchor('http://mysite.tumblr.com', 'target=_blank');
    } 
    $this->load->view('content', $data);
}

在我的视图中:

<div id="menu">
         <ul>
            <?php foreach ($tabMenu as $item) : ?>
                <?php
                    $url = "<li><a href='" . base_url(); 
                    $url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
                    $url .= strtoupper($item->name) . "</a></li>";
                    echo $url;
                ?>
            <?php endforeach; ?>    
        </ul>
    </div> <!-- end of Menu -->

I'm building a menu options, having issue in last option, The Anchor method doesn't work as a link popup a new window. Besides, in option 1 and 2, I repeat those codes which is not look great.

Is there a better way to optimize those codes? make it cleaner.

In my controller:

public function loadPage($name, $pageID) {
    $data['title'] = $this->tabPageData;
    $data['tabMenu'] = $this->model->getAllMenuItems(); 

    if ($name == 'portfolio-1') {
        // load portfolio 1, get the page content (photos) and its name
        $data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
        $data['pageName'] = $this->model->getPageNameByID($pageID);
    } elseif ($name == 'portfolio-2') {
        $data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
        $data['pageName'] = $this->model->getPageNameByID($pageID);
    } elseif ($name == 'contact') {
        // load Contact page
        $data['tabContact'] = $this->model->getContactByPageID($pageID);
    } else {
        // load a Blog site
        echo anchor('http://mysite.tumblr.com', 'target=_blank');
    } 
    $this->load->view('content', $data);
}

In my View:

<div id="menu">
         <ul>
            <?php foreach ($tabMenu as $item) : ?>
                <?php
                    $url = "<li><a href='" . base_url(); 
                    $url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
                    $url .= strtoupper($item->name) . "</a></li>";
                    echo $url;
                ?>
            <?php endforeach; ?>    
        </ul>
    </div> <!-- end of Menu -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-12-27 02:26:51

我建议您通过创建一个为导航生成列表项的辅助方法来清理视图。

将以下代码放入 application/helpers/ 中名为 navigation_helper.php 的文件中。

if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('build_list_item'))
{
    function build_list_item ($item) {
        $url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
        $url = base_url() . $url_item_name . "/". $item->cat_id;
        return '<li><a href="' . $url . '">' . strtoupper($item->name) . '</a></li>';
    }
}

确保您正在控制器中加载助手,或者如果您经常使用它,则自动加载它。

$this->load->helper('navigation_helper');

那么在你看来你可以这样做:

<div id="menu">
    <ul>
        <?php foreach ($tabMenu as $item): ?>
            <?php echo build_list_item($item); ?>
        <?php endforeach; ?>    
    </ul>
</div>

I would suggest that you clean up your view by creating a helper method that generates a list item for your navigation.

Put the following code in a file named navigation_helper.php in application/helpers/.

if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('build_list_item'))
{
    function build_list_item ($item) {
        $url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
        $url = base_url() . $url_item_name . "/". $item->cat_id;
        return '<li><a href="' . $url . '">' . strtoupper($item->name) . '</a></li>';
    }
}

Make sure you are loading the helper in your controller or autoloading it if you use it often.

$this->load->helper('navigation_helper');

Then in your view you could do this:

<div id="menu">
    <ul>
        <?php foreach ($tabMenu as $item): ?>
            <?php echo build_list_item($item); ?>
        <?php endforeach; ?>    
    </ul>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文