Total Pageviews

Tuesday, 11 February 2014

go语言的模板引擎-amber


Amber is an elegant templating engine for Go Programming Language,
 inspired from HAML and Jade.
 
import "github.com/eknkc/amber"
Amber is an elegant templating engine for Go Programming Language It is inspired from HAML and Jade

Tags

A tag is simply a word:
html
is converted to
<html></html>
It is possible to add ID and CLASS attributes to tags:
div#main
span.time
are converted to
<div id="main"></div>
<span class="time"></span>
Any arbitrary attribute name / value pair can be added this way:
a[href="http://www.google.com"]
You can mix multiple attributes together
a#someid[href="/"][title="Main Page"].main.link Click Link
gets converted to
<a id="someid" class="main link" href="/" title="Main Page">Click Link</a>
It is also possible to define these attributes within the block of a tag
a
    #someid
    [href="/"]
    [title="Main Page"]
    .main
    .link
    | Click Link

Doctypes

To add a doctype, use !!! or doctype keywords:
!!! transitional
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
or use doctype
doctype 5
// <!DOCTYPE html>
Available options: 5, default, xml, transitional, strict, frameset, 1.1, basic, mobile

Tag Content

For single line tag text, you can just append the text after tag name:
p Testing!
would yield
<p>Testing!</p>
For multi line tag text, or nested tags, use indentation:
html
    head
        title Page Title
    body
        div#content
            p
                | This is a long page content
                | These lines are all part of the parent p

                a[href="/"] Go To Main Page

Data

Input template data can be reached by key names directly. For example, assuming the template has been executed with following JSON data:
{
    "Name": "Ekin",
    "LastName": "Koc",
    "Repositories": [
        "amber",
        "dateformat"
    ],
    "Avatar": "/images/ekin.jpg",
    "Friends": 17
}
It is possible to interpolate fields using #{}
p Welcome #{Name}!
would print
<p>Welcome Ekin!</p>
Attributes can have field names as well
a[title=Name][href="/ekin.koc"]
would print
<a title="Ekin" href="/ekin.koc"></a>

Expressions

Amber can expand basic expressions. For example, it is possible to concatenate strings with + operator:
p Welcome #{Name + " " + LastName}
Arithmetic expressions are also supported:
p You need #{50 - Friends} more friends to reach 50!
Expressions can be used within attributes
img[alt=Name + " " + LastName][src=Avatar] 
 
from https://github.com/eknkc/amber 

trofaf项目(http://briteming.blogspot.co.uk/2014/02/linux-vpsgo-trofaf.html)的
examples目录里就包含了amber模板。