Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyanhui committed Feb 14, 2017
1 parent b609cf1 commit f2ab8d9
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 115 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,4 @@ third-party archives.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ It has been used in production environment in [bthub.io](http://bthub.io).

## Features

- High performance.
- Easy to use.
- Powerful. template `Extend` and `Include` supported.
- High performance.
- Auto compiling when files change.

## Performance

Hero is the fastest and least-memory used among currently known template engines
in the benchmark. For more details and benchmarks please come to [github.com/SlinSo/goTemplateBenchmark](https://github.com/SlinSo/goTemplateBenchmark).

<img src='http://i.imgur.com/93D7T5C.png' width="600">
<img src='http://i.imgur.com/EIGtYyF.png' width="600">

## Install

go get github.com/shiyanhui/hero
Expand Down Expand Up @@ -69,7 +77,7 @@ And assumes that they are all under `$GOPATH/src/app/template`
### users.html
```html
<%: func UserList(userList []string) []byte %>
<%: func UserList(userList []string) *bytes.Buffer %>

<%~ "index.html" %>

Expand Down Expand Up @@ -107,8 +115,11 @@ Then we write a http server in `$GOPATH/src/app/main.go`.
package main

import (
"app/template"
"net/http"

"app/template"

"github.com/shiyanhui/hero"
)

func main() {
Expand All @@ -118,7 +129,11 @@ func main() {
"Bob",
"Tom",
}
w.Write(template.UserList(userList))

buffer := template.UserList(userList)
defer hero.PutBuffer(buffer)

w.Write(buffer.Bytes())
})

http.ListenAndServe(":8080", nil)
Expand All @@ -133,8 +148,8 @@ There are only nine necessary kinds of statements, which are:
- Function Definition `<%: func define %>`
- Function definition statement defines the function which represents a html file.
- The function defined should return one and only one parameter `[]byte`.
- Example:`<%: func UserList(userList []string) []byte %>` , which we have mentioned in quick start.
- The function defined should return one and only one parameter `*bytes.Buffer`.
- Example:`<%: func UserList(userList []string) *bytes.Buffer %>`, which we have mentioned in quick start.
- Extend `<%~ "parent template" %>`
- Extend statement states the parent template the current template extends.
Expand All @@ -148,9 +163,7 @@ There are only nine necessary kinds of statements, which are:

- Import `<%! go code %>`
- Import statement imports the packages used in the defined function, and it also contains everything that is outside of the defined function.

- Import statement will NOT be inherited by child template.

- Example:

```go
Expand Down
52 changes: 40 additions & 12 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ Hero是一个高性能、强大并且易用的go模板引擎,工作原理是

## Features

- 高性能.
- 非常易用.
- 功能强大,支持模板继承和模板include.
- 高性能.
- 自动编译.

## Performance

Hero在目前已知的模板引擎中是速度是最快的,并且内存使用是最少的。下面是Benchmark
结果,关于更多的细节和Benchmarks请到[github.com/SlinSo/goTemplateBenchmark](https://github.com/SlinSo/goTemplateBenchmark)查看。

<img src='http://i.imgur.com/93D7T5C.png' width="600">
<img src='http://i.imgur.com/EIGtYyF.png' width="600">

## Install

go get github.com/shiyanhui/hero
Expand Down Expand Up @@ -63,7 +71,7 @@ example:
### users.html

```html
<%: func UserList(userList []string) []byte %>
<%: func UserList(userList []string) *bytes.Buffer %>

<%~ "index.html" %>

Expand Down Expand Up @@ -98,8 +106,11 @@ hero -source="$GOPATH/src/app/template"
package main

import (
"app/template"
"net/http"

"app/template"

"github.com/shiyanhui/hero"
)

func main() {
Expand All @@ -109,7 +120,11 @@ func main() {
"Bob",
"Tom",
}
w.Write(template.UserList(userList))

buffer := template.UserList(userList)
defer hero.PutBuffer(buffer)

w.Write(buffer.Bytes())
})

http.ListenAndServe(":8080", nil)
Expand All @@ -124,8 +139,8 @@ Hero总共有九种语句,他们分别是:

- 函数定义语句 `<%: func define %>`
- 该语句定义了该模板所对应的函数,如果一个模板中没有函数定义语句,那么最终结果不会生成对应的函数。
- 该函数必须返回一个`[]byte`参数。
- 例:`<%: func UserList(userList []string) []byte %>`
- 该函数必须返回一个`*bytes.Buffer`参数。
- 例:`<%: func UserList(userList []string) *bytes.Buffer %>`

- 模板继承语句 `<%~ "parent template" %>`
- 该语句声明要继承的模板。
Expand Down Expand Up @@ -206,23 +221,36 @@ Hero总共有九种语句,他们分别是:
%>
```

- 原生值语句 `<%== statement %>`
- 原生值语句 `<%==[t] variable %>`

- 该语句把变量转换为string

- `t`是变量的类型,hero会自动根据`t`来选择转换函数。`t`的待选值有:
- `b`: bool
- `i`: int, int8, int16, int32, int64
- `u`: byte, uint, uint8, uint16, uint32, uint64
- `f`: float32, float64
- `s`: string
- `bs`: []byte
- `v`: interface

注意:
- 如果`t`没有设置,那么`t`默认为`s`.
- 最好不要使用`v`,因为其对应的转换函数为`fmt.Sprintf("%v", variable)`,该函数很慢。

- 例:

```go
<%== a %>
<%== a + b %>
<%== Add(a, b) %>
<%== user.Name %>
<%== "hello" %>
<%==i 34 %>
<%==u Add(a, b) %>
<%==s user.Name %>
```

- 转义值语句 `<%= statement %>`

- 该语句把变量转换为string后,又通过`html.EscapesString`记性转义。

- `t`跟原生值中的`t`一样。
- 例:

```go
Expand Down
24 changes: 14 additions & 10 deletions bufferpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ type pool struct {

func newPool() *pool {
p := &pool{
pool: new(sync.Pool),
ch: make(chan *bytes.Buffer, buffSize),
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
ch: make(chan *bytes.Buffer, buffSize),
}

// It's faster with unused channel buffer in go1.7.
// TODO: need removed?
for i := 0; i < buffSize; i++ {
p.ch <- new(bytes.Buffer)
}
Expand All @@ -32,18 +38,16 @@ func newPool() *pool {
}

// GetBuffer returns a *bytes.Buffer from sync.Pool.
func GetBuffer() (buffer *bytes.Buffer) {
v := defaultPool.pool.Get()
if v == nil {
buffer = new(bytes.Buffer)
} else {
buffer = v.(*bytes.Buffer)
}
return
func GetBuffer() *bytes.Buffer {
return defaultPool.pool.Get().(*bytes.Buffer)
}

// PutBuffer puts a *bytes.Buffer to the sync.Pool.
func PutBuffer(buffer *bytes.Buffer) {
if buffer == nil {
return
}

buffer.Reset()
defaultPool.pool.Put(buffer)
}
2 changes: 1 addition & 1 deletion bufferpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func TestGetBuffer(t *testing.T) {
}

func TestPutBubber(t *testing.T) {
// test for whether panic
// test for panic
PutBuffer(buffer)
}
Loading

0 comments on commit f2ab8d9

Please sign in to comment.