All Projects → shiyanhui → Hero

shiyanhui / Hero

Licence: other
A handy, fast and powerful go template engine.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Hero

Awesome Twig
A curated list of amazingly awesome Twig extensions, snippets and tutorials
Stars: ✭ 63 (-95.76%)
Mutual labels:  template-engine
Html
A Virtual DOM based templating-engine for PHP
Stars: ✭ 86 (-94.21%)
Mutual labels:  template-engine
Materialtabstemplate
MaterialTabsTemplate is solely created to reduce the burden of writing same boiler plate codes for Tab creation in Android.
Stars: ✭ 92 (-93.8%)
Mutual labels:  template-engine
Solid
Liquid template engine in Elixir
Stars: ✭ 68 (-95.42%)
Mutual labels:  template-engine
Tuxedo
Tuxedo is a template language for Swift.
Stars: ✭ 80 (-94.61%)
Mutual labels:  template-engine
Metalsmith React Templates
A metalsmith plugin to render files using React / Preact / JSX based templates.
Stars: ✭ 90 (-93.94%)
Mutual labels:  template-engine
Liquor
Liquor is a safe sandboxing compiling template language for Ruby
Stars: ✭ 57 (-96.16%)
Mutual labels:  template-engine
Art Template
High performance JavaScript templating engine
Stars: ✭ 9,554 (+543.37%)
Mutual labels:  template-engine
Hydrogen
🎈 Hydrogen. Voted (by me) the world's lightest static-site generator built with TypeScript ❤ It uses 🔥 lit-html inspired templating for super duper performant template generation.
Stars: ✭ 80 (-94.61%)
Mutual labels:  template-engine
Emerald
HTML5 templating engine for Nimrod
Stars: ✭ 91 (-93.87%)
Mutual labels:  template-engine
Hepek
Web content generators in Scala. Intuitive, scalable, powerful.
Stars: ✭ 69 (-95.35%)
Mutual labels:  template-engine
Handlebars.java
Logic-less and semantic Mustache templates with Java
Stars: ✭ 1,204 (-18.92%)
Mutual labels:  template-engine
Gdb
Generic Data Binder (GDB) for jQuery is a framework agnostic and extremely easy to use 2 way data binder. GDB binds views and models in realtime with live two-way binding and no hefty framework necessary.
Stars: ✭ 90 (-93.94%)
Mutual labels:  template-engine
Yii2 Smarty
Yii 2 Smarty Extension.
Stars: ✭ 67 (-95.49%)
Mutual labels:  template-engine
Api2html
Using the data from your API, generate the HTML on the fly! Server-side rendering of the mustache templates
Stars: ✭ 97 (-93.47%)
Mutual labels:  template-engine
Tangular
A simple JavaScript template engine like Angular.js for websites or node.js
Stars: ✭ 58 (-96.09%)
Mutual labels:  template-engine
Jingoo
OCaml template engine almost compatible with jinja2
Stars: ✭ 86 (-94.21%)
Mutual labels:  template-engine
Eps
A templating engine for PowerShell
Stars: ✭ 108 (-92.73%)
Mutual labels:  template-engine
Geco
Simple code generator based on a console project, running on .Net core and using C# interpolated strings
Stars: ✭ 97 (-93.47%)
Mutual labels:  template-engine
Cheatsheet Maker
Cheetsheet (cheat sheet or quick reference) generator. Use it for guides, instructions or study. Made in Python 3
Stars: ✭ 91 (-93.87%)
Mutual labels:  template-engine

Hero

Hero is a handy, fast and powerful go template engine, which pre-compiles the html templates to go code. It has been used in production environment in bthub.io.

GoDoc Go Report Card Travis CI

中文文档

Features

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

Performance

Hero is the fastest and least-memory used among currently known template engines in the benchmark. The data of chart comes from https://github.com/SlinSo/goTemplateBenchmark. You can find more details and benchmarks from that project.

Install

go get github.com/shiyanhui/hero/hero

# Hero needs `goimports` to format the generated codes.
go get golang.org/x/tools/cmd/goimports

Usage

hero [options]

  -source string
        the html template file or dir (default "./")
  -dest string
        generated golang files dir, it will be the same with source if not set
  -extensions string
        source file extensions, comma splitted if many (default ".html")
  -pkgname template
        the generated template package name, default is template (default "template")
  -watch
        whether automatically compile when the source files change

example:
	hero -source="./"
	hero -source="$GOPATH/src/app/template" -dest="./" -extensions=".html,.htm" -pkgname="t" -watch

Quick Start

Assume that we are going to render a user list userlist.html. index.html is the layout, and user.html is an item in the list.

And assumes that they are all under $GOPATH/src/app/template

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>

    <body>
        <%@ body { %>
        <% } %>
    </body>
</html>

userlist.html

<%: func UserList(userList []string, buffer *bytes.Buffer) %>

<%~ "index.html" %>

<%@ body { %>
    <% for _, user := range userList { %>
        <ul>
            <%+ "user.html" %>
        </ul>
    <% } %>
<% } %>

user.html

<li>
    <%= user %>
</li>

Then we compile the templates to go code.

hero -source="$GOPATH/src/app/template"

We will get three new .go files under $GOPATH/src/app/template, i.e. index.html.go, user.html.go and userlist.html.go.

Then we write a http server in $GOPATH/src/app/main.go.

main.go

package main

import (
    "bytes"
    "net/http"

    "app/template"
)

func main() {
    http.HandleFunc("/users", func(w http.ResponseWriter, req *http.Request) {
        var userList = []string {
            "Alice",
            "Bob",
            "Tom",
        }

        // Had better use buffer pool. Hero exports `GetBuffer` and `PutBuffer` for this.
        //
        // For convenience, hero also supports `io.Writer`. For example, you can also define
        // the function to `func UserList(userList []string, w io.Writer) (int, error)`,
        // and then:
        //
        //   template.UserList(userList, w)
        //
        buffer := new(bytes.Buffer)
        template.UserList(userList, buffer)
        w.Write(buffer.Bytes())
    })

    http.ListenAndServe(":8080", nil)
}

At last, start the server and visit http://localhost:8080/users in your browser, we will get what we want!

Template syntax

There are only nine necessary kinds of statements, which are:

  • Function Definition <%: func define %>

    • Function definition statement defines the function which represents an html file.
    • The type of the last parameter in the function defined should be *bytes.Buffer for manual buffer management or io.Writer for automatic buffer management ( note: if using io.Writer you may optionally specify return values (int, error) to handle the result of io.Writer.Write). Hero will identify the parameter name automaticly.
    • Example:
      • <%: func UserList(userList []string, buffer *bytes.Buffer) %>
      • <%: func UserList(userList []string, w io.Writer) %>
      • <%: func UserList(userList []string, w io.Writer) (int, error) %>
  • Extend <%~ "parent template" %>

    • Extend statement states the parent template the current template extends.
    • The parent template should be quoted with "".
    • Example: <%~ "index.html" >, which we have mentioned in quick start, too.
  • Include <%+ "sub template" %>

    • Include statement includes a sub-template to the current template. It works like #include in C++.
    • The sub-template should be quoted with "".
    • Example: <%+ "user.html" >, which we also have mentioned in quick start.
  • 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:

      <%!
        import (
            "fmt"
            "strings"
        )
      
        var a int
      
        const b = "hello, world"
      
        func Add(a, b int) int {
            return a + b
        }
      
        type S struct {
            Name string
        }
      
        func (s S) String() string {
            return s.Name
        }
      %>
  • Block <%@ blockName { %> <% } %>

    • Block statement represents a block. Child template overwrites blocks to extend parent template.

    • Example:

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset="utf-8">
          </head>
      
          <body>
              <%@ body { %>
              <% } %>
          </body>
      </html>
  • Code <% go code %>

    • Code statement states all code inside the defined function. It's just go code.

    • Example:

      <% for _, user := range userList { %>
          <% if user != "Alice" { %>
              <%= user %>
          <% } %>
      <% } %>
      
      <%
          a, b := 1, 2
          c := Add(a, b)
      %>
  • Raw Value <%==[t] variable %>

    • Raw Value statement will convert the variable to string.

    • t is the type of variable, hero will find suitable converting method by t. Candidates of t are:

      • b: bool
      • i: int, int8, int16, int32, int64
      • u: byte, uint, uint8, uint16, uint32, uint64
      • f: float32, float64
      • s: string
      • bs: []byte
      • v: interface

      Note:

      • If t is not set, the value of t is s.
      • Had better not use v, cause when t=v, the converting method is fmt.Sprintf("%v", variable) and it is very slow.
    • Example:

      <%== "hello" %>
      <%==i 34  %>
      <%==u Add(a, b) %>
      <%==s user.Name %>
  • Escaped Value <%=[t] variable %>

    • Escaped Value statement is similar with Raw Value statement, but after converting, it will be escaped it with html.EscapesString.

    • t is the same as in Raw Value Statement.

    • Example:

      <%= a %>
      <%=i a + b %>
      <%=u Add(a, b) %>
      <%=bs []byte{1, 2} %>
  • Note <%# note %>

    • Note statement add notes to the template.
    • It will not be added to the generated go source.
    • Example: <# this is just a note example>.

License

Hero is licensed under the Apache License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].