Help us shape the future of Go! Take the 2026 Go Developer Survey

goingtpl

package module
v0.0.0-...-24019f4 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: MIT Imports: 3 Imported by: 5

README

goingtpl

Japanese here

goingtpl is template parser that supports file include and extends.
There is also a function to cache templates.

Table of content

Install

go get github.com/playree/goingtpl

Usage

Template include function

Write {{include "xxx.html"}} in the template file.

[parent.html]

<!DOCTYPE html>
<html><body>
    <h1>Test code</h1>
{{template "footer"}}{{include "footer.html"}}
</body></html>
[footer.html]

{{define "footer"}}
    <p>Footer</p>
{{end}}

All you have to do is parse the parent file with the prepared method.

tpl := template.Must(goingtpl.ParseFile("parent.html"))

After parsing.

<!DOCTYPE html>
<html><body>
    <h1>Test code</h1>
    <p>Footer</p>
</body></html>
Template extends function

Write {{extends "xxx.html"}} in the template file.

[base.html]

<!DOCTYPE html>
<html><body>
    <h1>{{template "title" .}}</h1>
    <div style="background-color: #ddf;">
        {{template "content" .}}
    </div>
    <p>Footer</p>
</body></html>
[page1.html]

{{extends "base.html"}}
{{define "title"}}Page1{{end}}
{{define "content"}}
    <p>This is Page1.</p>
{{end}}

Be sure to write {{extends "xxx.html"}} at the beginning.

All you have to do is parse the file with the prepared method.

tpl := template.Must(goingtpl.ParseFile("page1.html"))

After parsing.

<!DOCTYPE html>
<html><body>
    <h1>Page1</h1>
    <div style="background-color: #ddf;">
        <p>This is Page1.</p>
    </div>
    <p>Footer</p>
</body></html>
Template caching function

We call the following method beforehand.

goingtpl.EnableCache(true)

API

  • ParseFile(filename string) (*template.Template, error)
  • ParseFileFuncs(filename string, funcs template.FuncMap) (*template.Template, error)
  • SetBaseDir(dir string)
  • EnableCache(flg bool)
  • ClearCache()
  • AddFixedFunc(name string, fnc interface{})

Examples

https://github.com/playree/goingtpl/tree/master/example

example.go

package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/playree/goingtpl"
)

func main() {
	// goingtpl setting
	goingtpl.SetBaseDir("./templates")
	goingtpl.EnableCache(true)
	goingtpl.AddFixedFunc(
		"now",
		func() string {
			return time.Now().Format("2006-01-02 15:04:05")
		},
	)

	http.HandleFunc("/example1", handleExample1)
	http.HandleFunc("/example2", handleExample2)
	http.HandleFunc("/clear", handleClear)
	log.Fatal(http.ListenAndServe(":8088", nil))
}

func handleExample1(w http.ResponseWriter, r *http.Request) {
	start := time.Now().UnixNano()

	funcMap := template.FuncMap{
		"repeat": func(s string, i int) string {
			return strings.Repeat(s, i)
		}}
	tpl := template.Must(goingtpl.ParseFileFuncs("parent.html", funcMap))
	// If you do not add a function
	// e.g. goingtpl.ParseFile("xxx.html")

	m := map[string]string{
		"Date": time.Now().Format("2006-01-02"),
		"Time": time.Now().Format("15:04:05"),
	}
	err := tpl.Execute(w, m)
	if err != nil {
		panic(err)
	}

	fmt.Printf("ExecTime=%d MicroSec\n",
		(time.Now().UnixNano()-start)/int64(time.Microsecond))
}

func handleExample2(w http.ResponseWriter, r *http.Request) {
	start := time.Now().UnixNano()

	tpl := template.Must(goingtpl.ParseFileFuncs("page1.html", nil))

	// If you do not add a function
	// e.g. goingtpl.ParseFile("xxx.html")

	m := map[string]string{
		"Date": time.Now().Format("2006-01-02"),
		"Time": time.Now().Format("15:04:05"),
	}
	err := tpl.Execute(w, m)
	if err != nil {
		panic(err)
	}

	fmt.Printf("ExecTime=%d MicroSec\n",
		(time.Now().UnixNano()-start)/int64(time.Microsecond))
}

func handleClear(w http.ResponseWriter, r *http.Request) {
	goingtpl.ClearCache()
	fmt.Println("ClearCache")
}

templates/parent.html

<!DOCTYPE html>
<html><body>
    <div>parent.html</div>
    <div style="padding-left: 2rem;">
        {{template "child01" .}}{{include "parts/child01.html"}}
        {{template "child02-1" .}}{{include "parts/child02.html"}}
        {{template "child02-2" .}}
    </div>
</body></html>

templates/parts/child01.html

{{define "child01"}}
<div>
    child01.html {{.Date}} {{.Time}}
    <div style="padding-left: 2rem;">
        {{template "child03-1"}}{{include "parts/child03.html"}}
    </div>
</div>
{{end}}

templates/parts/child02.html

{{define "child02-1"}}
<div>child02.html - 1</div>
{{end}}

{{define "child02-2"}}
<div>
    child02.html - 2
    <div style="padding-left: 2rem;">
        {{template "child03-2" .}}{{include "parts/child03.html"}}
    </div>
</div>
{{end}}

templates/parts/child03.html

{{define "child03-1"}}
<i>child03.html - 1</i>
{{end}}

{{define "child03-2"}}
<i>child03.html - 2 </i><br>
Arguments = {{.Date}} {{.Time}}<br>
Func now = {{now}}<br>
Func repeat = {{repeat "A" 5}}
{{end}}

templates/parts/base.html

<!DOCTYPE html>
<html><body>
    <h1>{{template "title" .}}</h1>
    <p>This is a sample of extends.</p>
    <div style="background-color: #ddf;">
        {{template "content" .}}
    </div>
</body></html>

templates/page1.html

{{extends "parts/base.html"}}
{{define "title"}}Page1{{end}}
{{define "content"}}
    This is Page1.
    <p>
        Loaded {{.Date}} {{.Time}}
    </p>
{{end}}

Result of operation

Demo

Demo

Licence

MIT

Documentation

Overview

Package goingtpl is template parser that supports file inclusion

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFixedFunc

func AddFixedFunc(name string, fnc interface{})

AddFixedFunc Add a fixed function to use.

func ClearCache

func ClearCache()

ClearCache Clear the cached template.

func EnableCache

func EnableCache(flg bool)

EnableCache Enable template caching. When the cache function is enabled, the template once read is cached in memory. You can reduce disk access.

func GetBaseDir

func GetBaseDir() string

GetBaseDir Return BaseDir

func ParseFile

func ParseFile(filename string) (*template.Template, error)

ParseFile is template parser that supports file inclusion. If BaseDIr is specified, specify a file path based on BaseDir. e.g. {{include "xxx.html"}}

func ParseFileFuncs

func ParseFileFuncs(filename string, funcs template.FuncMap) (*template.Template, error)

ParseFileFuncs is template parser that supports file inclusion. If BaseDIr is specified, specify a file path based on BaseDir. e.g. {{include "xxx.html"}}

func ParseFuncs

func ParseFuncs(filename string, contents string, funcs template.FuncMap) (*template.Template, error)

ParseFuncs is template parser that supports file inclusion. If BaseDIr is specified, specify a file path based on BaseDir. e.g. {{include "xxx.html"}}

func SetBaseDir

func SetBaseDir(dir string)

SetBaseDir Specify the directory where the template is placed. By specifying it, you can shorten the template file specification.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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