[Gin][go] html에 전송한 값 출력하기

PersesTitan·2022년 7월 1일
0

Gin

목록 보기
3/7

우선 코드를 구현해봅시다.

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}}에서는 코드에서 보낸값을 꺼내게됩니다.

이제 한번 실행해보겠습니다.

재대로 출력되는 모습을 볼 수 있었습니다.


깃허브 링크

profile
안녕하세요 페르세스 티탄입니다! 부족한 부분이 많이 있겠지만 잘부탁드립니다.

0개의 댓글