우선 코드를 구현해봅시다.
package main
import (
"github.com/gin-gonic/gin"
"html/template"
"net/http"
)
func main() {
router := gin.Default()
router.SetFuncMap(template.FuncMap{})
router.LoadHTMLGlob("templates/*.html")
router.GET("/", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{
"title": "메인페이지 입니다.",
"message": "제목",
})
})
_ = router.Run(":8080")
}
위 코드에는 title과 message에 출력될 페이지를 전송하게 됩니다.
*templates는 링크를 참고해주세요.
{{template "header.html"}}
<h1 class="title">{{.title}}</h1>
<p>{{ .message}}</p>
{{template "footer.html"}}
{{.title}}과 {{.message}}에서는 코드에서 보낸값을 꺼내게됩니다.
이제 한번 실행해보겠습니다.
재대로 출력되는 모습을 볼 수 있었습니다.