go언어
글 수 53
2015.07.31 18:52:07 (*.134.169.166)
7131
Martini에는 martini-contrib라고 하는 여러가지의 plugin들을 제공합니다.
그중에 이번 글에서는 render를 소개할까 합니다.
공식 홈페이지는 https://github.com/martini-contrib/render 입니다.
go의 기본 모듈인 http의 template을 좀 더 익숙한 형태로 사용할 수 있는 contrib입니다.
뿐만 아니라 JSON, XML같은 형식으로 출력하여 RESTful API에서 사용할 수 있는 함수도 제공합니다.
제가 만든 간단한 소스를 보고 어떤 식으로 사용하는지 감을 잡는데 도움이 됐으면 합니다.
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func main() {
m := martini.Classic()
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
data := make(map[string]interface{})
data["hello"] = "안녕"
data["world"] = "세계"
data["value"] = 1
m.Get("/", func(r render.Render) {
r.HTML(200, "hello", data)
})
m.Get("/api", func(r render.Render) {
r.JSON(200, data)
})
m.Run()
}<html>
<head>
</head>
<body>
{{ yield }}
</body>
</html><!-- templates/hello.tmpl -->
<h2>Hello - {{ .hello }}!</h2>
<h2>World - {{ .world }}!</h2>
<h2>Value - {{ .value }}!</h2>


