项目作者: gookit

项目描述 :
A simple view renderer based on the `html/template`, but much simpler to use. support layout rendering, including templates. 简单的视图渲染工具包,基于原生的 html/template 包,支持布局文件渲染,支持加载多目录,多文件,渲染字符串模板等。
高级语言: Go
项目地址: git://github.com/gookit/view.git
创建时间: 2018-08-16T06:23:34Z
项目社区:https://github.com/gookit/view

开源协议:MIT License

关键词:
layout template template-rendering view-renderer

下载


EasyTpl

GoDoc
Coverage Status
Go Report Card
Unit-Tests

A simple template renderer based on the Go html/template, but much simpler to use.
Support layout rendering, including templates.

中文说明

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

Quick Start

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gookit/easytpl"
  6. )
  7. func main() {
  8. // equals to call: easytpl.NewRenderer() + r.MustInit()
  9. r := easytpl.NewInited(func(r *easytpl.Renderer) {
  10. // setting default layout
  11. r.Layout = "layout" // equals to "layout.tpl"
  12. // templates dir. will auto load on init.
  13. r.ViewsDir = "testdata"
  14. // add template function
  15. r.AddFunc("myFunc", func() string {
  16. return "my-func"
  17. })
  18. })
  19. // fmt.Println(r.TemplateNames(true))
  20. bf := new(bytes.Buffer)
  21. // render template string
  22. r.String(bf, `hello {{.}}`, "tom")
  23. fmt.Print(bf.String()) // hello tom
  24. // render template without layout
  25. r.Partial(bf, "home", "tom")
  26. bf.Reset()
  27. // render with default layout
  28. r.Render(bf, "home", "tom")
  29. bf.Reset()
  30. // render with custom layout
  31. r.Render(bf, "home", "tom", "site/layout")
  32. bf.Reset()
  33. // load named string template
  34. r.LoadString("my-page", "welcome {{.}}")
  35. // now, you can use "my-page" as an template name
  36. r.Partial(bf, "my-page", "tom") // welcome tom
  37. bf.Reset()
  38. // more ways for load templates
  39. r.LoadByGlob("some/path/*", "some/path")
  40. r.LoadFiles("path/file1.tpl", "path/file2.tpl")
  41. }

more APIs please GoDoc

Layout Example

basic layout structure:

  1. {{ include "part0" }}{{ yield }}{{ include "part1" }}

current template will render at {{ yield }}

example files:

  1. templates/
  2. |_ layouts/
  3. | |_ default.tpl
  4. | |_ header.tpl
  5. | |_ footer.tpl
  6. |_ home.tpl
  7. |_ about.tpl
  • layout: templates/layouts/default.tpl
  1. <html>
  2. <head>
  3. <title>layout example</title>
  4. </head>
  5. <body>
  6. <!-- include "layouts/header.tpl" -->
  7. {{ include "header" }}
  8. <!-- Render the current template here -->
  9. {{ yield }}
  10. <!-- include "layouts/footer.tpl" -->
  11. {{ include "footer" }}
  12. </body>
  13. </html>
  • templates/layouts/header.tpl
  1. <header>
  2. <h2>page header</h2>
  3. </header>
  • templates/layouts/footer.tpl
  1. <footer>
  2. <h2>page footer</h2>
  3. </footer>
  • templates/home.tpl

```gotemplate title=”home.tpl”

Hello, {{ .Name | upper }}


At template {{ current_tpl }}


Lorem ipsum dolor sit amet, consectetur adipisicing elit.

  1. ### Usage
  2. ```go
  3. v := easytpl.NewInited(func(r *easytpl.Renderer) {
  4. // setting default layout
  5. r.Layout = "layouts/default" // equals to "layouts/default.tpl"
  6. // templates dir. will auto load on init.
  7. r.ViewsDir = "templates"
  8. })
  9. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  10. v.Render(w, "home", easytpl.M{"Name": "tom"})
  11. })
  12. slog.Println("Listening port: 9100")
  13. 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

  1. templates/
  2. |_ base.tpl
  3. |_ home.tpl
  4. |_ about.tpl

templates/base.tpl base template contents:

```gotemplate title=”base.tpl”



layout example


{{ block “content” . }}

Hello, at base template


{{ end }}

  1. `templates/home.tpl` contents:
  2. ```gotemplate title="home.tpl"
  3. {{ extends "base" }}
  4. {{ define "content" }}
  5. <h1>Hello, {{ .Name | upper }}</h1>
  6. <h2>At template {{ current_tpl }}</h2>
  7. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  8. {{ end }}

Usage

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gookit/easytpl"
  5. "github.com/gookit/slog"
  6. )
  7. func main() {
  8. v := easytpl.NewExtends(easytpl.WithTplDirs("templates"))
  9. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  10. v.Render(w, "home", easytpl.M{"Name": "tom"})
  11. })
  12. slog.Info("Listening port: 9100")
  13. http.ListenAndServe(":9100", nil)
  14. }

Available Options

  1. // Debug setting
  2. Debug bool
  3. // Layout template name
  4. Layout string
  5. // Delims define for template
  6. Delims TplDelims
  7. // ViewsDir the default views directory, multi use "," split
  8. ViewsDir string
  9. // ExtNames allowed template extensions. eg {"tpl", "html"}
  10. ExtNames []string
  11. // FuncMap func map for template
  12. FuncMap template.FuncMap
  13. // DisableLayout disable layout. default is False
  14. DisableLayout bool
  15. // AutoSearchFile auto search template file, when not found on compiled templates. default is False
  16. AutoSearchFile bool

Apply options

  • method 1
  1. r := easytpl.NewRenderer()
  2. r.Layout = "layouts/default"
  3. // ... ...
  4. r.MustInit()
  • method 2
  1. r := easytpl.NewRenderer(func (r *Renderer) {
  2. r.Layout = "layouts/default"
  3. // ... ...
  4. })
  5. r.MustInit()
  • method 3
  1. r := easytpl.NewInited(func (r *Renderer) {
  2. r.Layout = "layouts/default"
  3. // ... ...
  4. })

Reference

License

MIT