rockpongo2

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

rock-pongo2

基于 pongo2(Django 模板语法)的 rock 视图引擎。与核心分离,独立维护。

安装

go get github.com/go-rock/rock-pongo2

使用

package main

import (
	"github.com/go-rock/rock"
	render "github.com/go-rock/rock-pongo2"
)

func main() {
	app := rock.New()

	app.RegisterView(render.New(render.ViewConfig{
		ViewDir:   "./views/",   // 模板目录
		Extension: ".html",      // 模板扩展名
	}))

	app.Get("/", func(c rock.Context) {
		c.HTML("home", rock.M{"title": "Home"}) // 渲染 views/home.html
	})

	app.Run(":8989")
}

配置

  • render.Default() —— 使用默认配置(ViewDir: "./views2/", Extension: ".html")
  • render.New(render.ViewConfig{...}) —— 自定义配置
  • ViewConfig.Box *pongo2.TemplateSet —— 可选,提供已加载的模板集(用于缓存/内存模板)
  • engine.SetViewDir(dir) / engine.GetViewDir() —— 运行时切换模板目录(可配合配置热更新)

模板数据

c.HTML 传入的数据会被转换为 pongo2.Context,支持:

  • rock.M / map[string]interface{} —— 直接转换
  • pongo2.Context —— 透传
  • struct / struct 指针 —— 按 json tag(无 tag 用字段名)转为 map
  • 其他类型 —— 返回空上下文,不会 panic

热更新示例

app.GetView().Engine.SetViewDir("./themes/" + theme + "/")

测试

go test ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ViewConfig

type ViewConfig struct {
	ViewDir   string
	Extension string
	Box       *pongo2.TemplateSet
}

type ViewEngine

type ViewEngine struct {
	Config ViewConfig
	// contains filtered or unexported fields
}

func Default

func Default() *ViewEngine

func New

func New(config ViewConfig) *ViewEngine

func (*ViewEngine) ExecuteWriter

func (r *ViewEngine) ExecuteWriter(writer io.Writer, filename string, bindingData interface{}) error

func (*ViewEngine) Ext

func (e *ViewEngine) Ext() string

func (*ViewEngine) GetViewDir

func (e *ViewEngine) GetViewDir() string

func (*ViewEngine) Name

func (e *ViewEngine) Name() string

func (*ViewEngine) SetViewDir

func (e *ViewEngine) SetViewDir(viewDir string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址