• Stars
    star
    241
  • Rank 161,488 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🧬 Template engine middleware for Fiber

Fiber

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

9 template engines are supported:

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

package main

import (
	"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 <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 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

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

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

	// run pkger && go build
}

packr

Read documentation: 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,
	})

	// run packr && go build
}

go.rice

Read documentation: 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,
	})

	// run rice embed-go && go build
}

fileb0x

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

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/template/html"
	// your generated package
	"github.com/<user>/<repo>/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

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

More Repositories

1

fiber

⚑️ Express inspired web framework written in Go
Go
30,851
star
2

recipes

πŸ“ Examples for πŸš€ Fiber
Go
2,814
star
3

awesome-fiber

✨ A curated list of awesome Fiber middlewares, boilerplates, recipes, articles and tools.
407
star
4

jwt

⚠️ Deprecated repository, available within Fiber Contrib.
Go
400
star
5

boilerplate

🚧 Boilerplate for πŸš€ Fiber
Go
353
star
6

swagger

🧬 fiber middleware to automatically generate RESTful API documentation with Swagger
Go
334
star
7

websocket

⚠️ Deprecated repository, available within Fiber Contrib.
Go
296
star
8

storage

πŸ“¦ Premade storage drivers for πŸš€ Fiber
Go
240
star
9

docs

πŸ“š Documentation for πŸš€ Fiber
JavaScript
210
star
10

adaptor

🧬 Adaptor middleware to convert net/http handlers from/to Fiber request handlers
Go
181
star
11

contrib

🧬 Repository for third party middlewares with dependencies
Go
176
star
12

cli

Fiber Command Line Interface
Go
82
star
13

helmet

🧬 Helmet middleware for Fiber
Go
81
star
14

keyauth

🧬 Key Authentication for Fiber
Go
77
star
15

utils

⚑ A collection of common functions but with better performance, less allocations and less dependencies created for Fiber.
Go
35
star
16

session

⚠ Deprecated, available within Fiber v2
Go
24
star
17

basicauth

⚠ Deprecated, available within Fiber v2
Go
17
star
18

website

🌈 The website of the πŸš€ Fiber framework
TypeScript
14
star
19

redirect

🧬 Redirect middleware for Fiber
Go
14
star
20

cors

⚠ Deprecated, available within Fiber v2
Go
14
star
21

rewrite

🧬 Rewrite middleware for Fiber
Go
12
star
22

embed

⚠ Deprecated, available within Fiber v2
Go
11
star
23

limiter

⚠ Deprecated, available within Fiber v2
Go
10
star
24

proxy

⚠ Deprecated, available within Fiber v2
Go
8
star
25

logger

⚠ Deprecated, available within Fiber v2
Go
7
star
26

csrf

⚠ Deprecated, available within Fiber v2
Go
5
star
27

pprof

⚠ Deprecated, available within Fiber v2
Go
5
star
28

recover

⚠ Deprecated, available within Fiber v2
Go
2
star
29

compression

⚠ Deprecated, available within Fiber v2
Go
1
star