👋Welcome

이 패키지는 Fiber 웹 프레임워크와 함께 여러 템플릿 엔진을 사용할 수 있는 universal methods를 제공합니다. > v1.11.1에서 새롭게 추가된 Views 인터페이스를 사용합니다. @bdtomlin & @arsmn의 도움에 특별히 감사드립니다!

9개의 템플릿 엔진이 지원됩니다:

Installation

Go 버전 1.17 이상이 필요합니다.

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/template/<원하는 템플릿 엔진>/vX

Examples

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"))
}

더 많은 예제

보다 구체적인 예제를 보려면 각 엔진 폴더를 방문하여 자세히 알아볼 수 있습니다

임베디드 시스템

http.FileSystem 인터페이스를 지원하므로 다른 라이브러리를 사용하여 내장 바이너리에서 템플릿을 로드할 수 있습니다.

pkger

문서 읽기: https://github.com/markbates/pkger

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 실행
}

packr

문서 읽기: https://github.com/gobuffalo/packr

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 실행
}

go.rice

문서 읽기: https://github.com/GeertJohan/go.rice

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 실행
}

fileb0x

문서 읽기: https://github.com/UnnoTed/fileb0x

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 사용법은 문서를 참조하세요
}

벤치마크

Simple

Simple 벤치마크 결과

Extended

Extended 벤치마크 결과

벤치마크는 Apple Macbook M1에서 실행되었습니다. 각 엔진은 20번 벤치마크를 수행하고 결과를 단일 xlsx 파일로 평균 내었습니다. Mustache는 확장 벤치마크에서 제외되었습니다.

Last updated