충분히 쌓여가는
Template 파일 만들기 본문
template 파일을 생성하는 위치는 templates 디렉터리
New -> Directory
디렉터리 이름은 layouts
header.mustache와 footer.mustache 파일 생성
greetings.mustache 코드에서 내비게이션 바 부분을 가져와 header.mustache 파일로 만든다
1행부터 </nav>까지 가져온다
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<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">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
greeting.mustache 파일에서 잘라낸 부분을 template로 대체해야 한다
View template 파일을 불러올 때 일반 변수를 {{변수명}}으로 사용하는 것과 달리
홑화살괄호(>)를 추가해 {{>파일명}}으로 작성한다
layouts 디렉터리 안에 header.mustache 파일을 만들었기 때문에 {{>layouts/header}}라고 작성한다
{{>layouts/header}}
<div class="bg-dark text-white p-5">
<h1>{{username}}님, 반갑습니다</h1>
</div>
<div class="mb-5 container-fluid">
<hr>
<p>BuildEnough | <a href="#">Pratice</a> | <a href="#">study</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
footer 부분도 마찬가지로 해준다
{{>layouts/footer}}
{{>layouts/header}}
<div class="bg-dark text-white p-5">
<h1>{{username}}님, 반갑습니다</h1>
</div>
{{>layouts/header}}
서버 실행
bye.mustache 도 동일 적용
최종 코드
greetings.mustache
{{>layouts/header}}
<div class="bg-dark text-white p-5">
<h1>{{username}}님, 반갑습니다</h1>
</div>
{{>layouts/footer}}
bye.mustache
{{>layouts/header}}
<div class="bg-dark text-white p-5">
<h1>{{nickname}}님, 안녕히 가세요</h1>
</div>
{{>layouts/footer}}
header.mustache
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<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">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
footer.mustache
<div class="mb-5 container-fluid">
<hr>
<p>BuildEnough | <a href="#">Pratice</a> | <a href="#">study</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
'Spring > 게시판 만들기' 카테고리의 다른 글
DTO를 데이터베이스에 저장하기 (0) | 2023.09.10 |
---|---|
form 데이터를 DTO로 받기 (0) | 2023.09.10 |
부스트스랩 사용 (0) | 2023.09.09 |
localhost:8080/bye 페이지 만들기 (0) | 2023.09.09 |
모델 추가하기 (0) | 2023.09.09 |