Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🧬 Template engine middleware for Fiber

License

NotificationsYou must be signed in to change notification settings

gofiber/template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

titledescriptionsidebar_position
👋 Welcome
🧬 Template engine middlewares for Fiber.
1

FiberFiber

This package provides universal methods to use multiple template engines with theFiber web framework using the newViews interface that is available from> v1.11.1. Special thanks to @bdtomlin & @arsmn for helping!

9 template engines are supported:

Installation

Go version1.17 or higher is required.

go get -u github.com/gofiber/fiber/v2go get -u github.com/gofiber/template/any_template_engine/vX

Example

package mainimport ("log""github.com/gofiber/fiber/v2"// To use a specific template engine, import as shown below:// "github.com/gofiber/template/pug"// "github.com/gofiber/template/mustache"// etc..// In this example we use the html template engine"github.com/gofiber/template/html/v2")funcmain() {// Create a new engine by passing the template folder// and template extension using <engine>.New(dir, ext string)engine:=html.New("./views",".html")// We also support the http.FileSystem interface// See examples below to load templates from embedded filesengine:=html.NewFileSystem(http.Dir("./views"),".html")// Reload the templates on each render, good for developmentengine.Reload(true)// Optional. Default: false// Debug will print each template that is parsed, good for debuggingengine.Debug(true)// Optional. Default: false// Layout defines the variable name that is used to yield templates within layoutsengine.Layout("embed")// Optional. Default: "embed"// Delims sets the action delimiters to the specified stringsengine.Delims("{{","}}")// Optional. Default: engine delimiters// AddFunc adds a function to the template's global function map.engine.AddFunc("greet",func(namestring)string {return"Hello, "+name+"!"})// After you created your engine, you can pass it to Fiber's Views Engineapp:=fiber.New(fiber.Config{Views:engine,})// To render a template, you can call the ctx.Render function// Render(tmpl string, values interface{}, layout ...string)app.Get("/",func(c*fiber.Ctx)error {returnc.Render("index", fiber.Map{"Title":"Hello, World!",})})// Render with layout exampleapp.Get("/layout",func(c*fiber.Ctx)error {returnc.Render("index", fiber.Map{"Title":"Hello, World!",},"layouts/main")})log.Fatal(app.Listen(":3000"))}

More Examples

To view more specific examples, you could visit each engine folder to learn more

embedded Systems

We support thehttp.FileSystem interface, so you can use different libraries to load the templates from embedded binaries.

pkger

Read documentation:https://github.com/markbates/pkger

package mainimport ("log""github.com/gofiber/fiber/v2""github.com/gofiber/template/html""github.com/markbates/pkger")funcmain() {engine:=html.NewFileSystem(pkger.Dir("/views"),".html")app:=fiber.New(fiber.Config{Views:engine,})// run pkger && go build}

packr

Read documentation:https://github.com/gobuffalo/packr

package mainimport ("log""github.com/gofiber/fiber/v2""github.com/gofiber/template/html""github.com/gobuffalo/packr/v2")funcmain() {engine:=html.NewFileSystem(packr.New("Templates","/views"),".html")app:=fiber.New(fiber.Config{Views:engine,})// run packr && go build}

go.rice

Read documentation:https://github.com/GeertJohan/go.rice

package mainimport ("log""github.com/gofiber/fiber/v2""github.com/gofiber/template/html""github.com/GeertJohan/go.rice")funcmain() {engine:=html.NewFileSystem(rice.MustFindBox("views").HTTPBox(),".html")app:=fiber.New(fiber.Config{Views:engine,})// run rice embed-go && go build}

fileb0x

Read documentation:https://github.com/UnnoTed/fileb0x

package mainimport ("log""github.com/gofiber/fiber/v2""github.com/gofiber/template/html"// your generated package"github.com/<user>/<repo>/static")funcmain() {engine:=html.NewFileSystem(static.HTTP,".html")app:=fiber.New(fiber.Config{Views:engine,})// Read the documentation on how to use fileb0x}

Benchmarks

Simple

Extended

Benchmarks were ran on Apple Macbook M1. Each engine was benchmarked 20 times and the results averaged into a single xlsx file. Mustache was excluded from the extended benchmark


[8]ページ先頭

©2009-2025 Movatter.jp