tpl

package module
v0.0.0-...-5e93b50 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 5 Imported by: 1

README

tpl

FOSSA Status

template like thymeleaf

模板 Templates

<p :text="${'Hello, ' + name}">Hello, World!</p>
Examples

see sub dir: example

代码 Codes

基本用法 Basic Usage
import (
	"code.gopub.tech/tpl"
	"code.gopub.tech/tpl/html"
	"code.gopub.tech/tpl/types"
)
// 解析模板文件夹
m := html.NewTplManager()
err := m.ParseWithSuffix(os.DirFS("path/to/tpl"),".html")
// 获取并执行模板
tpl, err := m.GetTemplate("index.html")
err = tpl.Execute(writer, map[string]any{})
另一种方式 HTMLRender
//go:embed views
var views embed.FS
var hotReload = false // gin.IsDebugging()
// 使用 HTMLRender 接口,支持热加载
r, err := tpl.NewHTMLRender(func() (types.TemplateManager, error) {
    m := html.NewTplManager()
    if hotReload {
        // 使用 os.DirFS 实时读取文件夹
        return m, m.ParseWithSuffix(os.DirFS("views"), ".html")
    }
    // 使用编译时嵌入的 embed.FS 资源
    return m, m.SetSubFS("views").ParseWithSuffix(views, ".html")
}, hotReload)
// 获取并执行模板
render := r.Instance("index.html", types.M{})
err := render.Render(writer)

如需支持 gettext 国际化(i18n)

Codes
import (
	"code.gopub.tech/tpl/exp"
	"code.gopub.tech/tpl/types"
	"github.com/youthlin/t"
)

err = tpl.Execute(writer, withI18n(req, map[string]any{
	"name": name,
}))
render := r.Instance("index.html", withI18n(req, types.M{
	"name": name,
}))

func withI18n(r *http.Request, data types.M) types.M {
	lang := t.GetUserLang(r)
	t := t.L(lang)
	if data == nil {
		data = types.M{}
	}
	data["t"] = t
	data["__"] = t.T
	data["_x"] = t.X
	data["_n"] = func(msgID, msgIDPlural string, n any, args ...interface{}) string {
		return t.N64(msgID, msgIDPlural, exp.ToNumber[int64](n), args...)
	}
	data["_xn"] = func(msgCtx, msgID, msgIDPlural string, n any, args ...interface{}) string {
		return t.XN64(msgCtx, msgID, msgIDPlural, exp.ToNumber[int64](n), args...)
	}
	return data
}


Templates
<p :text="${__('Hello, %s', name)}">Hello, World!</p>

Extract

see xtpl

go install code.gopub.tech/tpl/cmd/xtpl@latest
xtpl -help

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTMLRender

func NewHTMLRender(builder types.Factory, opts ...NewHTMLRenderOpt) (types.ReloadableRender, error)

NewHTMLRender 新建一个 HTML 渲染器

//go:embed views
var views embed.FS

var hotReload = gin.IsDebugging()
// NewHTMLRender
r, err := tpl.NewHTMLRender(func(context.Context) (types.TemplateManager, error) {
	m := html.NewTplManager()
	if hotReload {
		// 使用 os.DirFS 实时读取文件夹
		return m, m.ParseWithSuffix(os.DirFS("views"), ".html")
	}
	// 使用编译时嵌入的 embed.FS 资源
	return m, m.SetSubFS("views").ParseWithSuffix(views, ".html")
}, tpl.WithHotReload(hotReload))

ginS.Get("/", func(c *gin.Context) {
	// Instance
	c.Render(http.StatusOK, r.Instance(c, "index.html", gin.H{}))
})
ginS.Run()

func RenderToBytes

func RenderToBytes(tpl types.Template, data any) ([]byte, error)

RenderToString 执行一个模板 并将结果输出为字节数组

func RenderToString

func RenderToString(tpl types.Template, data any) (string, error)

RenderToString 执行一个模板 并将结果输出为字符串

Types

type NewHTMLRenderOpt

type NewHTMLRenderOpt func(*newHtmlRenderOpt)

func WithHotReload

func WithHotReload(hotReload bool) NewHTMLRenderOpt

WithHotReload 当需要热加载时 每次渲染都会重新解析模板. 出于性能考虑 应当仅在调试阶段开启; 正式环境如需重新解析模板 可以通过下方 Reload 方法触发.

Directories

Path Synopsis
cmd
xtpl Module
example module
exp

Jump to

Keyboard shortcuts

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