Views interface. This allows you to render HTML templates with dynamic data.
How Templates Work
Fiber uses aViews interface that template engines implement. You configure the engine when creating your app:
Views Interface
The Views interface requires two methods:Load(): Load/compile templates (called during app initialization)Render(): Render a template with data
Template Engines
Fiber works with multiple template engines through the template middleware package.Supported Engines
html
Go’s built-in html/template
django
Django-style templates
handlebars
Handlebars templates
mustache
Mustache templates
pug
Pug (formerly Jade)
amber
Amber templates
jet
Jet templates
ace
Ace templates
Using html/template
The most common engine is Go’s built-inhtml/template:
Basic Setup
Template File
views/index.html
Template Configuration
Directory Structure
Engine Options
Layouts
Use layouts for shared template structure:Layout Template
views/layouts/main.html
Using Layouts
Default Layout
Set a global default layout:Passing Data
Using fiber.Map
Using Structs
Passing Locals
Automatically passc.Locals() to templates:
ViewBind
Pre-bind data for templates:Template Functions
Add custom functions to templates:Using Functions
Reloading Templates
Enable auto-reload during development:Template Syntax Examples
Variables
Conditionals
Loops
Nested Data
Error Handling
Handle template rendering errors:Using Other Template Engines
Pug
views/index.pug
Handlebars
views/index.hbs
Best Practices
Use layouts for consistency
Use layouts for consistency
Define shared structure in layouts to avoid duplication.
Enable reload in development
Enable reload in development
Use template reloading during development for faster iteration.
Organize templates by feature
Organize templates by feature
Group related templates in subdirectories.
Cache templates in production
Cache templates in production
Disable reloading in production for better performance.
See Also
Context
Render templates from handlers
Template Package
Official template engines
Rendering
c.Render() API reference
Static Files
Serve static assets