객체로 간단한 부트스트랩 매뉴 제작 함수 > 개발자팁

개발자팁

개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.

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

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

본문

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

 

 

 

<!-- 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>

 

추천
1

댓글 0개

전체 5,352
개발자팁 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT