go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/template/<원하는 템플릿 엔진>/vX
package main
import (
"log"
"github.com/gofiber/fiber/v2"
// 특정 템플릿 엔진을 사용하려면 아래와 같이 import 합니다:
// "github.com/gofiber/template/pug"
// "github.com/gofiber/template/mustache"
// 등...
// 이 예제에서는 html 템플릿 엔진을 사용합니다
"github.com/gofiber/template/html/v2"
)
func main() {
// 템플릿 폴더와 템플릿 확장자를 전달하여 새 엔진을 생성합니다
// <engine>.New(dir, ext string)
engine := html.New("./views", ".html")
// http.FileSystem 인터페이스도 지원합니다
// 내장 파일에서 템플릿을 로드하는 예제는 아래를 참조하세요
engine := html.NewFileSystem(http.Dir("./views"), ".html")
// 각 렌더링마다 템플릿을 다시 로드합니다. 개발에 좋습니다
engine.Reload(true) // 선택사항. 기본값: false
// 디버그 모드를 활성화하면 각 템플릿이 파싱될 때마다 출력됩니다. 디버깅에 좋습니다
engine.Debug(true) // 선택사항. 기본값: false
// Layout은 레이아웃 내에서 템플릿을 yield하는 데 사용되는 변수 이름을 정의합니다
engine.Layout("embed") // 선택사항. 기본값: "embed"
// Delims는 action delimiters를 지정된 문자열로 설정합니다
engine.Delims("{{", "}}") // 선택사항. 기본값: 엔진 delimiters
// AddFunc는 템플릿의 전역 함수 맵에 함수를 추가합니다
engine.AddFunc("greet", func(name string) string {
return "Hello, " + name + "!"
})
// 엔진을 생성한 후에는 Fiber의 Views Engine에 전달할 수 있습니다
app := fiber.New(fiber.Config{
Views: engine,
})
// 템플릿을 렌더링하려면 ctx.Render 함수를 호출합니다
// Render(tmpl string, values interface{}, layout ...string)
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})
// 레이아웃 예제와 함께 렌더링
app.Get("/layout", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
}, "layouts/main")
})
log.Fatal(app.Listen(":3000"))
}
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/markbates/pkger"
)
func main() {
engine := html.NewFileSystem(pkger.Dir("/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// pkger && go build 실행
}
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/gobuffalo/packr/v2"
)
func main() {
engine := html.NewFileSystem(packr.New("Templates", "/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// packr && go build 실행
}
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/GeertJohan/go.rice"
)
func main() {
engine := html.NewFileSystem(rice.MustFindBox("views").HTTPBox(), ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// rice embed-go && go build 실행
}
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
// 생성된 패키지
"github.com/<사용자>/<저장소>/static"
)
func main() {
engine := html.NewFileSystem(static.HTTP, ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
// fileb0x 사용법은 문서를 참조하세요
}
벤치마크는 Apple Macbook M1에서 실행되었습니다. 각 엔진은 20번 벤치마크를 수행하고 결과를 단일 xlsx 파일로 평균 내었습니다. Mustache는 확장 벤치마크에서 제외되었습니다.