Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gookit/easytpl
Simple and easy-to-use template renderer, based on Golang html/template package. 简单易用的模板渲染工具库,基于Golang的 html/template 包,支持布局文件渲染,支持加载多目录,多文件,渲染字符串模板等。
https://github.com/gookit/easytpl
layout template template-engine template-layout template-rendering view-renderer
Last synced: about 2 months ago
JSON representation
Simple and easy-to-use template renderer, based on Golang html/template package. 简单易用的模板渲染工具库,基于Golang的 html/template 包,支持布局文件渲染,支持加载多目录,多文件,渲染字符串模板等。
- Host: GitHub
- URL: https://github.com/gookit/easytpl
- Owner: gookit
- License: mit
- Created: 2018-08-16T06:23:34.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-11T01:57:18.000Z (10 months ago)
- Last Synced: 2024-06-19T02:04:33.291Z (7 months ago)
- Topics: layout, template, template-engine, template-layout, template-rendering, view-renderer
- Language: Go
- Homepage:
- Size: 116 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EasyTpl
[![GoDoc](https://pkg.go.dev/badge/github.com/gookit/easytpl.svg)](https://pkg.go.dev/github.com/gookit/easytpl)
[![Coverage Status](https://coveralls.io/repos/github/gookit/easytpl/badge.svg?branch=master)](https://coveralls.io/github/gookit/easytpl?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/easytpl)](https://goreportcard.com/report/github.com/gookit/easytpl)
[![Unit-Tests](https://github.com/gookit/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/easytpl/actions)A simple template renderer based on the Go `html/template`, but much simpler to use.
Support layout rendering, including templates.> **[中文说明](README.zh-CN.md)**
## Features
- simple to use
- support loading multiple directories, multiple files
- support rendering string templates, etc.
- support layout render.
- eg `{{ include "header" }} {{ yield }} {{ include "footer" }}`
- support include other templates. eg `{{ include "other" }}`
- support `extends` base templates. eg `{{ extends "base.tpl" }}`
- support custom template functions
- built-in some helper methods `row`, `lower`, `upper`, `join` ...## Godoc
- [godoc for github](https://pkg.go.dev/github.com/gookit/easytpl)
## Quick Start
```go
package mainimport (
"bytes"
"fmt"
"github.com/gookit/easytpl"
)func main() {
// equals to call: easytpl.NewRenderer() + r.MustInit()
r := easytpl.NewInited(func(r *easytpl.Renderer) {
// setting default layout
r.Layout = "layout" // equals to "layout.tpl"
// templates dir. will auto load on init.
r.ViewsDir = "testdata"
// add template function
r.AddFunc("myFunc", func() string {
return "my-func"
})
})// fmt.Println(r.TemplateNames(true))
bf := new(bytes.Buffer)
// render template string
r.String(bf, `hello {{.}}`, "tom")
fmt.Print(bf.String()) // hello tom// render template without layout
r.Partial(bf, "home", "tom")
bf.Reset()// render with default layout
r.Render(bf, "home", "tom")
bf.Reset()// render with custom layout
r.Render(bf, "home", "tom", "site/layout")
bf.Reset()
// load named string template
r.LoadString("my-page", "welcome {{.}}")
// now, you can use "my-page" as an template name
r.Partial(bf, "my-page", "tom") // welcome tom
bf.Reset()
// more ways for load templates
r.LoadByGlob("some/path/*", "some/path")
r.LoadFiles("path/file1.tpl", "path/file2.tpl")
}
```> more APIs please [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl)
## Layout Example
basic layout structure:
```text
{{ include "part0" }}{{ yield }}{{ include "part1" }}
```> current template will render at `{{ yield }}`
example files:
```text
templates/
|_ layouts/
| |_ default.tpl
| |_ header.tpl
| |_ footer.tpl
|_ home.tpl
|_ about.tpl
```- **layout:** `templates/layouts/default.tpl`
```html
layout example
{{ include "header" }}
{{ yield }}
{{ include "footer" }}
```
- `templates/layouts/header.tpl`
```html
page header
```
- `templates/layouts/footer.tpl`
```html
page footer
```
- `templates/home.tpl`
```gotemplate title="home.tpl"
Hello, {{ .Name | upper }}
At template {{ current_tpl }}
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
```### Usage
```go
v := easytpl.NewInited(func(r *easytpl.Renderer) {
// setting default layout
r.Layout = "layouts/default" // equals to "layouts/default.tpl"
// templates dir. will auto load on init.
r.ViewsDir = "templates"
})http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
v.Render(w, "home", easytpl.M{"Name": "tom"})
})
slog.Println("Listening port: 9100")
http.ListenAndServe(":9100", nil)
```## `extends` example
A base template can be inherited using the `{{ extends "base.tpl" }}` statement.
> Note: The `extends` statement must be on the first line of the template file
```text
templates/
|_ base.tpl
|_ home.tpl
|_ about.tpl
````templates/base.tpl` base template contents:
```gotemplate title="base.tpl"
layout example
{{ block "content" . }}
Hello, at base template
{{ end }}
```
`templates/home.tpl` contents:
```gotemplate title="home.tpl"
{{ extends "base" }}{{ define "content" }}
Hello, {{ .Name | upper }}
At template {{ current_tpl }}
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
{{ end }}
```### Usage
```go
package mainimport (
"net/http"
"github.com/gookit/easytpl"
"github.com/gookit/slog"
)func main() {
v := easytpl.NewExtends(easytpl.WithTplDirs("templates"))http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
v.Render(w, "home", easytpl.M{"Name": "tom"})
})
slog.Info("Listening port: 9100")
http.ListenAndServe(":9100", nil)
}
```## Available Options
```go
// Debug setting
Debug bool
// Layout template name
Layout string
// Delims define for template
Delims TplDelims
// ViewsDir the default views directory, multi use "," split
ViewsDir string
// ExtNames allowed template extensions. eg {"tpl", "html"}
ExtNames []string
// FuncMap func map for template
FuncMap template.FuncMap
// DisableLayout disable layout. default is False
DisableLayout bool
// AutoSearchFile auto search template file, when not found on compiled templates. default is False
AutoSearchFile bool
```### Apply options
- method 1
```go
r := easytpl.NewRenderer()
r.Layout = "layouts/default"
// ... ...
r.MustInit()
```- method 2
```go
r := easytpl.NewRenderer(func (r *Renderer) {
r.Layout = "layouts/default"
// ... ...
})
r.MustInit()
```- method 3
```go
r := easytpl.NewInited(func (r *Renderer) {
r.Layout = "layouts/default"
// ... ...
})
```## Reference
- https://github.com/unrolled/render
- https://github.com/thedevsaddam/renderer## License
**MIT**