GoHTML Jet Package
中文 | English
The jet package provides a wrapper for the Jet template engine, offering a fast and feature-rich template engine with Django/Jinja2-like syntax.
Features
- ⚡ High Performance - One of the fastest Go template engines
- 🎨 Rich Syntax - Django/Jinja2-style template syntax
- 🔧 Extensible - Easy to add custom functions and filters
- 📦 embed.FS Support - Compile templates into binary for production
- 🌐 Template Inheritance - Built-in support for template inheritance
Installation
go get github.com/h3go/gohtml
go get github.com/CloudyKit/jet/v6
Quick Start
Basic Example
Create template file templates/index.jet:
<!DOCTYPE html>
<html>
<head>
<title>{{ Title }}</title>
</head>
<body>
<h1>{{ Message }}</h1>
<p>Current time: {{ now() }}</p>
</body>
</html>
Go code:
package main
import (
"bytes"
"fmt"
"github.com/h3go/gohtml/jet"
)
func main() {
// Create engine
engine := jet.New()
// Load templates from directory
engine.JoinDir("./templates")
// Render template
var buf bytes.Buffer
err := engine.Render(&buf, "index.jet", map[string]any{
"Title": "Welcome",
"Message": "Hello, Jet!",
})
if err != nil {
panic(err)
}
fmt.Println(buf.String())
}
Template Inheritance
Parent template templates/layout.jet:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
<style>
{% block style %}{% endblock %}
</style>
</head>
<body>
<header>
<h1>My Website</h1>
</header>
<main>
{% block content %}{% endblock %}
</main>
<footer>
<p>© 2024 My Website</p>
</footer>
</body>
</html>
Child template templates/home.jet:
{% extends "layout.jet" %}
{% block title %}Home - My Website{% endblock %}
{% block style %}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
{% endblock %}
{% block content %}
<h2>Welcome to Home Page</h2>
<p>User: {{ Username }}</p>
<p>Today is {{ now() }}</p>
{% endblock %}
Go code:
engine := jet.New()
engine.JoinDir("./templates")
var buf bytes.Buffer
engine.Render(&buf, "home.jet", map[string]any{
"Username": "John",
})
Using embed.FS (Production)
package main
import (
"embed"
"github.com/h3go/gohtml/jet"
)
//go:embed templates/*.jet
var templateFS embed.FS
func main() {
engine := jet.New()
// Load templates from embedded filesystem
engine.JoinFS(templateFS)
var buf bytes.Buffer
engine.Render(&buf, "index.jet", map[string]any{
"Title": "Production",
})
}
Development Mode
Enable development mode to disable template caching:
import "github.com/CloudyKit/jet/v6"
engine := jet.New(jet.InDevelopmentMode())
engine.JoinDir("./templates")
API Documentation
Engine
import "github.com/h3go/gohtml/jet"
// Create new engine instance
// options: Jet configuration options (e.g., jet.InDevelopmentMode())
func New(options ...jet.Option) *Engine
// Load templates from fs.FS (supports embed.FS)
func (e *Engine) JoinFS(fsys fs.FS)
// Load templates from directory
func (e *Engine) JoinDir(dir string)
// Add global variable accessible to all templates
func (e *Engine) AddGlobal(key string, value any)
// Add global function callable from all templates
func (e *Engine) AddGlobalFunc(key string, fn jet.Func)
// Render template to writer
func (e *Engine) Render(w io.Writer, name string, data map[string]any) error
Advanced Usage
Custom Global Variables
engine := jet.New()
engine.JoinDir("./templates")
// Add global variables
engine.AddGlobal("siteName", "My Website")
engine.AddGlobal("version", "1.0.0")
Use in templates:
<h1>{{ siteName }}</h1>
<p>Version: {{ version }}</p>
Custom Functions
import (
"reflect"
"github.com/CloudyKit/jet/v6"
"github.com/h3go/gohtml/jet"
)
engine := jet.New()
// Add custom function
engine.AddGlobalFunc("formatPrice", func(args jet.Arguments) reflect.Value {
price := args.Get(0).Float()
formatted := fmt.Sprintf("$%.2f", price)
return reflect.ValueOf(formatted)
})
engine.JoinDir("./templates")
Use in templates:
<p>Price: {{ formatPrice(99.99) }}</p>
Jet Template Syntax
Variables
{{ variable }}
{{ object.field }}
{{ array[0] }}
Control Structures
{% if condition %}
...
{% else if otherCondition %}
...
{% else %}
...
{% endif %}
{% for item in items %}
{{ item }}
{% endfor %}
{% for key, value in map %}
{{ key }}: {{ value }}
{% endfor %}
Filters
{{ text | upper }}
{{ text | lower }}
{{ text | trim }}
{{ number | int }}
{{ number | float }}
{* This is a comment *}
Production Best Practices
- Use embed.FS: Compile templates into binary
- Disable Development Mode: Don't use
jet.InDevelopmentMode() in production
- Template Caching: Jet automatically caches compiled templates
package main
import (
"embed"
"github.com/h3go/gohtml/jet"
)
//go:embed templates/*.jet
var templateFS embed.FS
func newEngine() *jet.Engine {
engine := jet.New() // Production mode (caching enabled)
engine.JoinFS(templateFS)
return engine
}
Development Hot Reload
import "github.com/CloudyKit/jet/v6"
func newEngine() *jet.Engine {
engine := jet.New(jet.InDevelopmentMode()) // Disable caching
engine.JoinDir("./templates")
return engine
}
Resources
License
MIT License - See LICENSE