더북(TheBook)

Index 뷰 작성

app/views/Post 디렉터리에 Index.html 파일을 생성하고 Index 뷰 코드를 작성해 보자.

▼ app/views/Post/Index.html

{{set . "title" "All Posts"}}
{{template "header.html" .}}
  
<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>Title</th>
            <th>Created at</th>
            <th>Updated at</th>
        </tr>
    </thead>
    <tbody>
        {{range .posts}}
            <tr>
                <th scope="row">{{.Id}}</th>
                <td>{{.Title}}</td>
                <td><time>{{.CreatedAt}}</time></td>
                <td><time>{{.UpdatedAt}}</time></td>
            </tr>
        {{end}}
    </tbody>
</table>
  
{{template "footer.html" .}}

Index 뷰에서는 posts 배열을 순환하여 내용과 링크를 보여준다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.