Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gofiber/template
🧬 Template engine middleware for Fiber
https://github.com/gofiber/template
fiber middleware template template-engine
Last synced: 3 days ago
JSON representation
🧬 Template engine middleware for Fiber
- Host: GitHub
- URL: https://github.com/gofiber/template
- Owner: gofiber
- License: mit
- Created: 2020-02-14T01:58:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-13T18:53:25.000Z (4 months ago)
- Last Synced: 2024-08-04T01:06:14.754Z (3 months ago)
- Topics: fiber, middleware, template, template-engine
- Language: Go
- Homepage: https://docs.gofiber.io/guide/templates
- Size: 1.15 MB
- Stars: 258
- Watchers: 7
- Forks: 53
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-fiber - template - This package contains 8 template engines that can be used with Fiber v1.10.x Go version 1.13 or higher is required. (⚙️ Middlewares / 🔗 External)
README
---
title: 👋 Welcome
description: 🧬 Template engine middlewares for Fiber.
sidebar_position: 1
---
This package provides universal methods to use multiple template engines with the [Fiber web framework](https://github.com/gofiber/fiber) using the new [Views](https://godoc.org/github.com/gofiber/fiber#Views) interface that is available from `> v1.11.1`. Special thanks to @bdtomlin & @arsmn for helping!
9 template engines are supported:
- [ace](./ace/README.md)
- [amber](./amber/README.md)
- [django](./django/README.md)
- [handlebars](./handlebars/README.md)
- [html](./html/README.md)
- [jet](./jet/README.md)
- [mustache](./mustache/README.md)
- [pug](./pug/README.md)
- [slim](./slim/README.md)### Installation
> Go version `1.17` or higher is required.```
go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/template/any_template_engine/vX
```### Example
```go
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"
)func main() {
// Create a new engine by passing the template folder
// and template extension using .New(dir, ext string)
engine := html.New("./views", ".html")// We also support the http.FileSystem interface
// See examples below to load templates from embedded files
engine := html.NewFileSystem(http.Dir("./views"), ".html")// Reload the templates on each render, good for development
engine.Reload(true) // Optional. Default: false// Debug will print each template that is parsed, good for debugging
engine.Debug(true) // Optional. Default: false// Layout defines the variable name that is used to yield templates within layouts
engine.Layout("embed") // Optional. Default: "embed"// Delims sets the action delimiters to the specified strings
engine.Delims("{{", "}}") // Optional. Default: engine delimiters// AddFunc adds a function to the template's global function map.
engine.AddFunc("greet", func(name string) string {
return "Hello, " + name + "!"
})// After you created your engine, you can pass it to Fiber's Views Engine
app := 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 {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})// Render with layout example
app.Get("/layout", func(c *fiber.Ctx) error {
return c.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
- [ace](./ace/README.md)
- [amber](./amber/README.md)
- [django](./django/README.md)
- [handlebars](./handlebars/README.md)
- [html](./html/README.md)
- [jet](./jet/README.md)
- [mustache](./mustache/README.md)
- [pug](./pug/README.md)
- [slim](./slim/README.md)### embedded Systems
We support the `http.FileSystem` interface, so you can use different libraries to load the templates from embedded binaries.
#### pkger
Read documentation: https://github.com/markbates/pkger```go
package mainimport (
"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,
})// run pkger && go build
}
```
#### packr
Read documentation: https://github.com/gobuffalo/packr```go
package mainimport (
"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,
})// run packr && go build
}
```
#### go.rice
Read documentation: https://github.com/GeertJohan/go.rice```go
package mainimport (
"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,
})// run rice embed-go && go build
}```
#### fileb0x
Read documentation: https://github.com/UnnoTed/fileb0x```go
package mainimport (
"log""github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
// your generated package
"github.com///static"
)func main() {
engine := html.NewFileSystem(static.HTTP, ".html")app := fiber.New(fiber.Config{
Views: engine,
})// Read the documentation on how to use fileb0x
}
```### Benchmarks
#### Simple
![](https://raw.githubusercontent.com/gofiber/template/master/.github/data/Simple-TimeperOperation.png)#### Extended
![](https://raw.githubusercontent.com/gofiber/template/master/.github/data/Extended-TimeperOperation.png)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