pongo2 模板引擎
来源:pongo2 模板引擎 /
时间:2026-07-08
pongo2 is a Django-syntax compatible template engine for Go.
https://pongo2.dev/
Features
Django Compatible - Familiar syntax for Django developers
Advanced Expressions - C-like expressions with arithmetic, comparison, and logical operators
Template Inheritance -
extends,block, andincludefor DRY templatesMacros - Reusable template fragments with arguments and default values
Extensible - Custom filters and tags
Sandbox Mode - Restrict available tags and filters
Multiple Loaders - Load templates from filesystem, embedded files, HTTP, or custom sources
Quick Start
Installation
go get -u github.com/flosch/pongo2/v7Hello World
package main
import (
"fmt"
"github.com/flosch/pongo2/v7"
)
func main() {
// These functions use the DefaultSet (convenient for simple use cases)
tpl, _ := pongo2.FromString("Hello {{ name }}!")
out, _ := tpl.Execute(pongo2.Context{"name": "World"})
fmt.Println(out) // Hello World!
}Template File
// Using DefaultSet (simple, but has unrestricted filesystem access)
tpl := pongo2.Must(pongo2.FromFile("templates/page.html"))
err := tpl.ExecuteWriter(pongo2.Context{
"title": "My Page",
"items": []string{"one", "two", "three"},
}, responseWriter)
下一篇: pongo2 样例
