Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gookit/easytpl

Open more actions menu

Repository files navigation

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

package main

import (
	"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

Layout Example

basic layout structure:

{{ include "part0" }}{{ yield }}{{ include "part1" }}

current template will render at {{ yield }}

example files:

templates/
  |_ layouts/
  |    |_ default.tpl
  |    |_ header.tpl
  |    |_ footer.tpl
  |_ home.tpl
  |_ about.tpl
  • layout: templates/layouts/default.tpl
<html>
  <head>
    <title>layout example</title>
  </head>
  <body>
    <!-- include "layouts/header.tpl" -->
    {{ include "header" }}
    <!-- Render the current template here -->
    {{ yield }}
    <!-- include "layouts/footer.tpl" -->
    {{ include "footer" }}
  </body>
</html>
  • templates/layouts/header.tpl
<header>
    <h2>page header</h2>
</header>
  • templates/layouts/footer.tpl
<footer>
    <h2>page footer</h2>
</footer>
  • templates/home.tpl
  <h1>Hello, {{ .Name | upper }}</h1>
  <h2>At template {{ current_tpl }}</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>

Usage

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

templates/
  |_ base.tpl
  |_ home.tpl
  |_ about.tpl

templates/base.tpl base template contents:

<html lang="en">
  <head>
    <title>layout example</title>
  </head>
  <body>
    {{ block "content" . }}
    <h1>Hello, at base template</h1>
    {{ end }}
  </body>
</html>

templates/home.tpl contents:

{{ extends "base" }}

{{ define "content" }}
  <h1>Hello, {{ .Name | upper }}</h1>
  <h2>At template {{ current_tpl }}</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
{{ end }}

Usage

package main

import (
    "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

// 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
r := easytpl.NewRenderer()
r.Layout = "layouts/default"
// ... ...
r.MustInit()
  • method 2
r := easytpl.NewRenderer(func (r *Renderer) {
	r.Layout = "layouts/default"
	// ... ...
})
r.MustInit()
  • method 3
r := easytpl.NewInited(func (r *Renderer) {
	r.Layout = "layouts/default" 
	// ... ...
})

Reference

License

MIT

About

Simple and easy-to-use template renderer, based on Golang html/template package. 简单易用的模板渲染工具库,基于Golang的 html/template 包,支持布局文件渲染,支持加载多目录,多文件,渲染字符串模板等。

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.