source: code/trunk/vendor/github.com/eknkc/amber/README.md@ 67

Last change on this file since 67 was 67, checked in by Izuru Yakumo, 23 months ago

Use vendored modules

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 11.7 KB
RevLine 
[67]1# amber [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/eknkc/amber) [![Build Status](https://travis-ci.org/eknkc/amber.svg?branch=master)](https://travis-ci.org/eknkc/amber)
2
3## Notice
4> While Amber is perfectly fine and stable to use, I've been working on a direct Pug.js port for Go. It is somewhat hacky at the moment but take a look at [Pug.go](https://github.com/eknkc/pug) if you are looking for a [Pug.js](https://github.com/pugjs/pug) compatible Go template engine.
5
6### Usage
7```go
8import "github.com/eknkc/amber"
9```
10
11Amber is an elegant templating engine for Go Programming Language
12It is inspired from HAML and Jade
13
14### Tags
15
16A tag is simply a word:
17
18 html
19
20is converted to
21
22```html
23<html></html>
24```
25
26It is possible to add ID and CLASS attributes to tags:
27
28 div#main
29 span.time
30
31are converted to
32
33```html
34<div id="main"></div>
35<span class="time"></span>
36```
37
38Any arbitrary attribute name / value pair can be added this way:
39
40 a[href="http://www.google.com"]
41
42You can mix multiple attributes together
43
44 a#someid[href="/"][title="Main Page"].main.link Click Link
45
46gets converted to
47
48```html
49<a id="someid" class="main link" href="/" title="Main Page">Click Link</a>
50```
51
52It is also possible to define these attributes within the block of a tag
53
54 a
55 #someid
56 [href="/"]
57 [title="Main Page"]
58 .main
59 .link
60 | Click Link
61
62### Doctypes
63
64To add a doctype, use `!!!` or `doctype` keywords:
65
66 !!! transitional
67 // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
68
69or use `doctype`
70
71 doctype 5
72 // <!DOCTYPE html>
73
74Available options: `5`, `default`, `xml`, `transitional`, `strict`, `frameset`, `1.1`, `basic`, `mobile`
75
76### Tag Content
77
78For single line tag text, you can just append the text after tag name:
79
80 p Testing!
81
82would yield
83
84 <p>Testing!</p>
85
86For multi line tag text, or nested tags, use indentation:
87
88 html
89 head
90 title Page Title
91 body
92 div#content
93 p
94 | This is a long page content
95 | These lines are all part of the parent p
96
97 a[href="/"] Go To Main Page
98
99### Data
100
101Input template data can be reached by key names directly. For example, assuming the template has been
102executed with following JSON data:
103
104```json
105{
106 "Name": "Ekin",
107 "LastName": "Koc",
108 "Repositories": [
109 "amber",
110 "dateformat"
111 ],
112 "Avatar": "/images/ekin.jpg",
113 "Friends": 17
114}
115```
116
117It is possible to interpolate fields using `#{}`
118
119 p Welcome #{Name}!
120
121would print
122
123```html
124<p>Welcome Ekin!</p>
125```
126
127Attributes can have field names as well
128
129 a[title=Name][href="/ekin.koc"]
130
131would print
132
133```html
134<a title="Ekin" href="/ekin.koc"></a>
135```
136
137### Expressions
138
139Amber can expand basic expressions. For example, it is possible to concatenate strings with + operator:
140
141 p Welcome #{Name + " " + LastName}
142
143Arithmetic expressions are also supported:
144
145 p You need #{50 - Friends} more friends to reach 50!
146
147Expressions can be used within attributes
148
149 img[alt=Name + " " + LastName][src=Avatar]
150
151### Variables
152
153It is possible to define dynamic variables within templates,
154all variables must start with a $ character and can be assigned as in the following example:
155
156 div
157 $fullname = Name + " " + LastName
158 p Welcome #{$fullname}
159
160If you need to access the supplied data itself (i.e. the object containing Name, LastName etc fields.) you can use `$` variable
161
162 p $.Name
163
164### Conditions
165
166For conditional blocks, it is possible to use `if <expression>`
167
168 div
169 if Friends > 10
170 p You have more than 10 friends
171 else if Friends > 5
172 p You have more than 5 friends
173 else
174 p You need more friends
175
176Again, it is possible to use arithmetic and boolean operators
177
178 div
179 if Name == "Ekin" && LastName == "Koc"
180 p Hey! I know you..
181
182There is a special syntax for conditional attributes. Only block attributes can have conditions;
183
184 div
185 .hasfriends ? Friends > 0
186
187This would yield a div with `hasfriends` class only if the `Friends > 0` condition holds. It is
188perfectly fine to use the same method for other types of attributes:
189
190 div
191 #foo ? Name == "Ekin"
192 [bar=baz] ? len(Repositories) > 0
193
194### Iterations
195
196It is possible to iterate over arrays and maps using `each`:
197
198 each $repo in Repositories
199 p #{$repo}
200
201would print
202
203 p amber
204 p dateformat
205
206It is also possible to iterate over values and indexes at the same time
207
208 each $i, $repo in Repositories
209 p
210 .even ? $i % 2 == 0
211 .odd ? $i % 2 == 1
212
213### Mixins
214
215Mixins (reusable template blocks that accept arguments) can be defined:
216
217 mixin surprise
218 span Surprise!
219 mixin link($href, $title, $text)
220 a[href=$href][title=$title] #{$text}
221
222and then called multiple times within a template (or even within another mixin definition):
223
224 div
225 +surprise
226 +surprise
227 +link("http://google.com", "Google", "Check out Google")
228
229Template data, variables, expressions, etc., can all be passed as arguments:
230
231 +link(GoogleUrl, $googleTitle, "Check out " + $googleTitle)
232
233### Imports
234
235A template can import other templates using `import`:
236
237 a.amber
238 p this is template a
239
240 b.amber
241 p this is template b
242
243 c.amber
244 div
245 import a
246 import b
247
248gets compiled to
249
250 div
251 p this is template a
252 p this is template b
253
254### Inheritance
255
256A template can inherit other templates. In order to inherit another template, an `extends` keyword should be used.
257Parent template can define several named blocks and child template can modify the blocks.
258
259 master.amber
260 !!! 5
261 html
262 head
263 block meta
264 meta[name="description"][content="This is a great website"]
265
266 title
267 block title
268 | Default title
269 body
270 block content
271
272 subpage.amber
273 extends master
274
275 block title
276 | Some sub page!
277
278 block append meta
279 // This will be added after the description meta tag. It is also possible
280 // to prepend someting to an existing block
281 meta[name="keywords"][content="foo bar"]
282
283 block content
284 div#main
285 p Some content here
286
287### License
288(The MIT License)
289
290Copyright (c) 2012 Ekin Koc <ekin@eknkc.com>
291
292Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
293
294The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
295
296THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
297
298## Usage
299
300```go
301var DefaultOptions = Options{true, false}
302var DefaultDirOptions = DirOptions{".amber", true}
303```
304
305#### func Compile
306
307```go
308func Compile(input string, options Options) (*template.Template, error)
309```
310Parses and compiles the supplied amber template string. Returns corresponding Go
311Template (html/templates) instance. Necessary runtime functions will be injected
312and the template will be ready to be executed.
313
314#### func CompileFile
315
316```go
317func CompileFile(filename string, options Options) (*template.Template, error)
318```
319Parses and compiles the contents of supplied filename. Returns corresponding Go
320Template (html/templates) instance. Necessary runtime functions will be injected
321and the template will be ready to be executed.
322
323#### func CompileDir
324```go
325func CompileDir(dirname string, dopt DirOptions, opt Options) (map[string]*template.Template, error)
326```
327Parses and compiles the contents of a supplied directory name. Returns a mapping of template name (extension stripped) to corresponding Go Template (html/template) instance. Necessary runtime functions will be injected and the template will be ready to be executed.
328
329If there are templates in subdirectories, its key in the map will be it's path relative to `dirname`. For example:
330```
331templates/
332 |-- index.amber
333 |-- layouts/
334 |-- base.amber
335```
336```go
337templates, err := amber.CompileDir("templates/", amber.DefaultDirOptions, amber.DefaultOptions)
338templates["index"] // index.amber Go Template
339templates["layouts/base"] // base.amber Go Template
340```
341By default, the search will be recursive and will match only files ending in ".amber". If recursive is turned off, it will only search the top level of the directory. Specified extension must start with a period.
342
343#### type Compiler
344
345```go
346type Compiler struct {
347 // Compiler options
348 Options
349}
350```
351
352Compiler is the main interface of Amber Template Engine. In order to use an
353Amber template, it is required to create a Compiler and compile an Amber source
354to native Go template.
355
356 compiler := amber.New()
357 // Parse the input file
358 err := compiler.ParseFile("./input.amber")
359 if err == nil {
360 // Compile input file to Go template
361 tpl, err := compiler.Compile()
362 if err == nil {
363 // Check built in html/template documentation for further details
364 tpl.Execute(os.Stdout, somedata)
365 }
366 }
367
368#### func New
369
370```go
371func New() *Compiler
372```
373Create and initialize a new Compiler
374
375#### func (*Compiler) Compile
376
377```go
378func (c *Compiler) Compile() (*template.Template, error)
379```
380Compile amber and create a Go Template (html/templates) instance. Necessary
381runtime functions will be injected and the template will be ready to be
382executed.
383
384#### func (*Compiler) CompileString
385
386```go
387func (c *Compiler) CompileString() (string, error)
388```
389Compile template and return the Go Template source You would not be using this
390unless debugging / checking the output. Please use Compile method to obtain a
391template instance directly.
392
393#### func (*Compiler) CompileWriter
394
395```go
396func (c *Compiler) CompileWriter(out io.Writer) (err error)
397```
398Compile amber and write the Go Template source into given io.Writer instance You
399would not be using this unless debugging / checking the output. Please use
400Compile method to obtain a template instance directly.
401
402#### func (*Compiler) Parse
403
404```go
405func (c *Compiler) Parse(input string) (err error)
406```
407Parse given raw amber template string.
408
409#### func (*Compiler) ParseFile
410
411```go
412func (c *Compiler) ParseFile(filename string) (err error)
413```
414Parse the amber template file in given path
415
416#### type Options
417
418```go
419type Options struct {
420 // Setting if pretty printing is enabled.
421 // Pretty printing ensures that the output html is properly indented and in human readable form.
422 // If disabled, produced HTML is compact. This might be more suitable in production environments.
423 // Defaukt: true
424 PrettyPrint bool
425 // Setting if line number emitting is enabled
426 // In this form, Amber emits line number comments in the output template. It is usable in debugging environments.
427 // Default: false
428 LineNumbers bool
429}
430```
431
432#### type DirOptions
433
434```go
435// Used to provide options to directory compilation
436type DirOptions struct {
437 // File extension to match for compilation
438 Ext string
439 // Whether or not to walk subdirectories
440 Recursive bool
441}
442```
Note: See TracBrowser for help on using the repository browser.