template

package
v2.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2015 License: MIT, MIT Imports: 8 Imported by: 0

README

template

general purpose templating based on go-on/replacer

Performance

replacing 5000 placeholders that occur 1x in the template

BenchmarkTemplateStandardLibM   20  52983126 ns/op   5.4x (html/template)
BenchmarkTemplateGoOnM         200   9801185 ns/op   1.0x (go-on/template)

replacing 2 placeholders that occur 2500x in the template

BenchmarkTemplateStandardLib    50  23802659 ns/op  25.3x (html/template)
BenchmarkTemplateGoOn         2000    942400 ns/op   1.0x (go-on/template)

replacing 2 placeholders that occur 1x in the template, parsing template each time

BenchmarkOnceTemplateStandardLib   50000  70096 ns/op 10.7x (html/template)
BenchmarkOnceTemplateGoOn         500000   6574 ns/op  1.0x (go-on/template)

Usage

package main

import (
    "bytes"
    "fmt"
    "html"

    "github.com/go-on/template"
    "github.com/go-on/template/placeholder"
)

func Html(name string) (t placeholder.Placeholder) {
    return template.NewPlaceholder(name)
}

func Text(name string) (t placeholder.Placeholder) {
    return template.NewPlaceholder(
        name,
        func(in interface{}) (out string) { return html.EscapeString(in.(string)) },
    )
}

var (
    person   = Text("person")
    greeting = Html("greeting")
    T        = template.New("t").MustAdd("<h1>Hi, ", person, "</h1>", greeting).Parse()
)

func main() {
    fmt.Println(
        T.MustReplace(
            person.Set("S<o>meone"),
            greeting.Set("<div>Hi</div>"),
        ),
    )

    var buffer bytes.Buffer
    for i := 0; i < 10; i++ {
        T.MustReplaceTo(&buffer,
            person.Setf("Bugs <Bunny> %v", i+1),
            greeting.Set("<p>How are you?</p>\n"))
    }
    fmt.Println(buffer.String())
}

returns

<h1>Hi, S&lt;o&gt;meone</h1><div>Hi</div>
<h1>Hi, Bugs &lt;Bunny&gt; 1</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 2</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 3</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 4</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 5</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 6</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 7</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 8</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 9</h1><p>How are you?</p>
<h1>Hi, Bugs &lt;Bunny&gt; 10</h1><p>How are you?</p>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPlaceholder

func NewPlaceholder(name string, escaper ...func(interface{}) string) placeholder.Placeholder

Types

type Buffer

type Buffer struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

a named buffer, fullfills the Setter interface

func (*Buffer) Name

func (b *Buffer) Name() string

func (*Buffer) Print

func (b *Buffer) Print()

func (*Buffer) Println

func (b *Buffer) Println()

func (*Buffer) ServeHTTP

func (bf *Buffer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP writes the content of the buffer to the responsewriter, each time it is called. It does not clean the buffer as would be done with WriteTo(ResponseWriter)

func (*Buffer) SetString

func (b *Buffer) SetString() string

type Escaper

type Escaper map[string]func(interface{}) string

func (Escaper) View

func (esc Escaper) View(stru interface{}, tag string) *View

type Template

type Template struct {
	*Buffer
	// contains filtered or unexported fields
}

func New

func New(name string) *Template

func (*Template) Add

func (t *Template) Add(data ...interface{}) (err error)

func (*Template) Handle

TODO check how it could work for subtemplates and multi replacements and how it could integrate with context and router and integrate it with html lib

func (*Template) MustAdd

func (t *Template) MustAdd(data ...interface{}) *Template

add data to the template

func (*Template) MustWrite

func (t *Template) MustWrite(b []byte)

func (*Template) MustWriteByte

func (t *Template) MustWriteByte(b byte)

func (*Template) MustWriteRune

func (t *Template) MustWriteRune(r rune)

func (*Template) MustWriteSetter

func (t *Template) MustWriteSetter(s placeholder.Setter)

func (*Template) MustWriteString

func (t *Template) MustWriteString(s string)

func (*Template) MustWriteTo

func (t *Template) MustWriteTo(w io.Writer)

func (*Template) New

func (t *Template) New() *Buffer

func (*Template) Parse

func (t *Template) Parse() *Template

func (*Template) Replace

func (r *Template) Replace(setters ...placeholder.Setter) (bf *Buffer)

func (*Template) ReplaceMixed

func (r *Template) ReplaceMixed(mixed ...interface{}) (bf *Buffer)

func (*Template) ReplaceMixedTo

func (r *Template) ReplaceMixedTo(b *bytes.Buffer, mixed ...interface{}) (bf *Buffer)

func (*Template) ReplaceTo

func (r *Template) ReplaceTo(b *bytes.Buffer, setters ...placeholder.Setter) (bf *Buffer)

func (*Template) ServeHTTP

func (t *Template) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Template) String

func (t *Template) String() string

func (*Template) WriteSetter

func (t *Template) WriteSetter(p placeholder.Setter) (err error)

type TemplatePlaceholder

type TemplatePlaceholder struct {
	Value   string
	Escaper []func(interface{}) string
	// contains filtered or unexported fields
}

func (TemplatePlaceholder) Name

func (p TemplatePlaceholder) Name() string

func (TemplatePlaceholder) Set

func (p TemplatePlaceholder) Set(val interface{}) placeholder.Setter

func (TemplatePlaceholder) SetString

func (p TemplatePlaceholder) SetString() string

func (TemplatePlaceholder) Setf

func (p TemplatePlaceholder) Setf(format string, vals ...interface{}) placeholder.Setter

func (TemplatePlaceholder) WriteTo

func (p TemplatePlaceholder) WriteTo(w io.Writer) (int64, error)

type View

type View struct {
	// contains filtered or unexported fields
}

func (*View) HasPlaceholder

func (str *View) HasPlaceholder(field string) bool

func (*View) Placeholder

func (str *View) Placeholder(field string) placeholder.Placeholder

func (*View) Set

func (str *View) Set(stru interface{}) (ss []placeholder.Setter)

func (*View) Tag

func (str *View) Tag() string

func (*View) Type

func (str *View) Type() string

Directories

Path Synopsis
_examples
fat
sub

Jump to

Keyboard shortcuts

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