객체로 간단한 부트스트랩 매뉴 제작 함수 정보
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개