테스트 사이트 - 개발 중인 베타 버전입니다

객체로 간단한 부트스트랩 매뉴 제작 함수

· 2년 전 · 686

아직 초보여서 오류가 있으면 알려주세요^^

 

 

 

<!-- index.php -->

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Menu demo</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
  </head>
  <body>
    <?php
    $MENUS = [
        [
            "name" => "home",
            "href" => "/"
        ],
        [
            "name" => "dropdown test",
            "dropdown" => [
                [
                    "name" => "test",
                    "href" => "/test"
                ],
                [
                    "name" => "dropdown test",
                    "dropdown" => [
                        [
                            "name" => "test1",
                            "href" => "/test1"
                        ]
                    ]
                ]
            ]
        ]
    ];
    
    function dropdownMenu($active, $menus) {
        $html = '';
        $isActive = false;
    
        foreach ($menus as $element) {
            if (!isset($element['dropdown'])) {
                $menuHref = strtolower(trim($element['href'], '/'));
                $activeHref = strtolower(trim($active, '/'));
    
                $activeMenu = $menuHref === $activeHref;
                if ($activeMenu) $isActive = true;
    
                $html .= '<li><a class="dropdown-item' . ($activeMenu ? " active" : "") . '" href="' . $element['href'] . '">' . $element['name'] . '</a></li>';
            } else {
                $menuData =Menu($active, $element['dropdown']);
                if ($menuData[1]) $isActive = true;
    
                $html .= '<li class="dropdown dropend"><a class="dropdown-item dropdown-toggle' . ($menuData[1] ? " active" : "") . '" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">' . $element['name'] . '</a><ul class="dropdown-menu">' . $menuData[0] . '</ul></li>';
            }
        }
    
        return [
            $html,
           $isActive
        ];
    }
    
    function menu($active, $menus) {
        $html = '';
        $isActive = false;
    
        foreach ($menus as $element) {
            if (!isset($element['dropdown'])) {
                $menuHref = strtolower(trim($element['href'], '/'));
                $activeHref = strtolower(trim($active, '/'));
    
                $activeMenu = $menuHref === $activeHref;
                if ($activeMenu) $isActive = true;
    
                $html .= '<li class="nav-item"><a class="nav-link' . ($activeMenu ? " active" : "") . '" aria-current="page" href="' . $element['href'] . '">' . $element['name'] . '</a></li>';
            } else {
                $menuData = dropdownMenu($active, $element['dropdown']);
                if ($menuData[1]) $isActive = true;
    
                $html .= '<li class="nav-item dropdown"><a class="nav-link dropdown-toggle' . ($menuData[1] ? " active" : "") . '" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">' . $element['name'] . '</a><ul class="dropdown-menu">' . $menuData[0] . '</ul></li>';
            }
        }
    
        return [
            $html,
            $isActive
        ];
    }
    ?>
    <nav class="navbar navbar-expand-lg bg-body-tertiary">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav me-auto mb-2 mb-lg-0">
            <? echo menu($_SERVER['REQUEST_URI'], $MENUS)[0] ?>
          </ul>
        </div>
      </div>
    </nav>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
  </body>
</html>

 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
17191
17162
17160
17158
17156
17155
17153
17151
17145
17135
17131
17125
17114
17107
17099
17096
17089
17088
17082
17079
17078
17077
17070
17068
17067
17063
17060
17048
17045
17044