html

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

html

HTML render for http client.

Usage

html "github.com/xiaoenai/tp-micro/helper/mod-html"

Test
package html_test

import (
	"testing"
	"time"

	html "github.com/xiaoenai/tp-micro/helper/mod-html"
)

type meta struct{}

func (m *meta) SetMeta(key, value string) {}

func TestParseText(t *testing.T) {
	const (
		doc1 = `<!DOCTYPE html>
	<html>
	<head>
	    <title>html test 1</title>
	</head>
	<body>
	<br/>
	<h2><center>{{.}}</center></h2>
	</body>
	</html>`
		doc2 = `<!DOCTYPE html>
	<html>
	<head>
	    <title>html test 2</title>
	</head>
	<body>
	<br/>
	<h2><center>{{.}}</center></h2>
	</body>
	</html>`
	)

	err := html.Parse("a", doc1)
	if err != nil {
		t.Fatal(err)
	}
	err = html.Parse("b", doc2)
	if err != nil {
		t.Fatal(err)
	}

	b, rerr := html.Render(new(meta), "a", "TestParse A!")
	t.Logf("body: %s, rerr: %v", b, rerr)
	b, rerr = html.Render(new(meta), "b", "TestParse B!")
	t.Logf("body: %s, rerr: %v", b, rerr)
}

func TestParseFiles(t *testing.T) {
	err := html.ParseFiles("../mod-html/a_test.tpl", "../mod-html/b_test.tpl")
	if err != nil {
		t.Fatal(err)
	}

	b, rerr := html.Render(new(meta), "a_test.tpl", "TestParseFiles A!")
	t.Logf("body: %s, rerr: %v", b, rerr)
	b, rerr = html.Render(new(meta), "b_test.tpl", "TestParseFiles B!")
	t.Logf("body: %s, rerr: %v", b, rerr)
}

func TestParseGlob(t *testing.T) {
	err := html.ParseGlob("../mod-html/*.tpl")
	if err != nil {
		t.Fatal(err)
	}

	b, rerr := html.Render(new(meta), "a_test.tpl", "TestParseGlob A!")
	t.Logf("body: %s, rerr: %v", b, rerr)
	b, rerr = html.Render(new(meta), "b_test.tpl", "TestParseGlob B!")
	t.Logf("body: %s, rerr: %v", b, rerr)
}

func TestDelims(t *testing.T) {
	const (
		doc = `<!DOCTYPE html>
	<html>
	<head>
	    <title>html test 1</title>
	</head>
	<body>
	<br/>
	<h2><center>{{{.}}}</center></h2>
	</body>
	</html>`
	)

	html.Delims("{{{", "}}}")
	err := html.Parse("doc", doc)
	if err != nil {
		t.Fatal(err)
	}
	b, rerr := html.Render(new(meta), "doc", "TestDelims!")
	t.Logf("body: %s, rerr: %v", b, rerr)
}

func TestGoTimingRefresh(t *testing.T) {
	err := html.ParseFiles("../mod-html/a_test.tpl")
	if err != nil {
		t.Fatal(err)
	}
	err = html.ParseGlob("../mod-html/*.tpl")
	if err != nil {
		t.Fatal(err)
	}

	b, rerr := html.Render(new(meta), "a_test.tpl", "TestGoTimingRefresh 1!")
	t.Logf("test1: body: %s, rerr: %v", b, rerr)

	html.GoTimingRefresh(time.Second * 5)
	time.Sleep(time.Second * 15)

	b, rerr = html.Render(new(meta), "a_test.tpl", "TestGoTimingRefresh 2!")
	t.Logf("test2: body: %s, rerr: %v", b, rerr)
}

test command:

go test -v -run=TestParseText
go test -v -run=TestParseFiles
go test -v -run=TestParseGlob
go test -v -run=TestDelims
go test -v -run=TestGoTimingRefresh

Documentation

Overview

Package html is HTML render for http client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delims

func Delims(left, right string)

Delims sets the action delimiters to the specified strings, to be used in subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the template, so calls can be chained.

Note: Must be called before Parse, ParseFiles, and ParseGlob.

func Funcs

func Funcs(funcMap template.FuncMap)

Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed. It panics if a value in the map is not a function with appropriate return type or if the name cannot be used syntactically as a function in a template. It is legal to overwrite elements of the map. The return value is the template, so calls can be chained.

func GoTimingRefresh

func GoTimingRefresh(d time.Duration)

GoTimingRefresh runs a goroutine that periodically refreshes template files.

func Option

func Option(opt ...string)

Option sets options for the template. Options are described by strings, either a simple string or "key=value". There can be at most one equals sign in an option string. If the option string is unrecognized or otherwise invalid, Option panics.

Known options:

missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map.

"missingkey=default" or "missingkey=invalid"
	The default behavior: Do nothing and continue execution.
	If printed, the result of the index operation is the string
	"<no value>".
"missingkey=zero"
	The operation returns the zero value for the map type's element.
"missingkey=error"
	Execution stops immediately with an error.

func Parse

func Parse(name, text string) error

Parse parses text as a template body for t. Named template definitions ({{define ...}} or {{block ...}} statements) in text define additional templates associated with t and are removed from the definition of t itself.

Templates can be redefined in successive calls to Parse. A template definition with a body containing only white space and comments is considered empty and will not replace an existing template's body. This allows using Parse to add new named template definitions without overwriting the main template body.

func ParseFiles

func ParseFiles(filenames ...string) (err error)

ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file. Since the templates created by ParseFiles are named by the base names of the argument files, t should usually have the name of one of the (base) names of the files. If it does not, depending on t's contents before calling ParseFiles, t.Execute may fail. In that case use t.ExecuteTemplate to execute a valid template.

When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results.

func ParseGlob

func ParseGlob(pattern string) (err error)

ParseGlob creates a new Template and parses the template definitions from the files identified by the pattern, which must match at least one file. The returned template will have the (base) name and (parsed) contents of the first file matched by the pattern. ParseGlob is equivalent to calling ParseFiles with the list of files matched by the pattern.

When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results.

The pattern syntax is:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-Separator characters
	'?'         matches any single non-Separator character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.

On Windows, escaping is disabled. Instead, '\\' is treated as path separator.

func Render

func Render(ctxMeta CtxMeta, tmplName string, data interface{}) ([]byte, *tp.Rerror)

Render renders the initialized html template by name.

Types

type CtxMeta

type CtxMeta interface {
	// SetMeta sets the header metadata 'key=value' for reply packet.
	SetMeta(key, value string)
}

CtxMeta the metadata method sets of context

Jump to

Keyboard shortcuts

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