66
|
1 # amber [](http://godoc.org/github.com/eknkc/amber) [](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
|
|
8 import "github.com/eknkc/amber"
|
|
9 ```
|
|
10
|
|
11 Amber is an elegant templating engine for Go Programming Language
|
|
12 It is inspired from HAML and Jade
|
|
13
|
|
14 ### Tags
|
|
15
|
|
16 A tag is simply a word:
|
|
17
|
|
18 html
|
|
19
|
|
20 is converted to
|
|
21
|
|
22 ```html
|
|
23 <html></html>
|
|
24 ```
|
|
25
|
|
26 It is possible to add ID and CLASS attributes to tags:
|
|
27
|
|
28 div#main
|
|
29 span.time
|
|
30
|
|
31 are converted to
|
|
32
|
|
33 ```html
|
|
34 <div id="main"></div>
|
|
35 <span class="time"></span>
|
|
36 ```
|
|
37
|
|
38 Any arbitrary attribute name / value pair can be added this way:
|
|
39
|
|
40 a[href="http://www.google.com"]
|
|
41
|
|
42 You can mix multiple attributes together
|
|
43
|
|
44 a#someid[href="/"][title="Main Page"].main.link Click Link
|
|
45
|
|
46 gets converted to
|
|
47
|
|
48 ```html
|
|
49 <a id="someid" class="main link" href="/" title="Main Page">Click Link</a>
|
|
50 ```
|
|
51
|
|
52 It 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
|
|
64 To 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
|
|
69 or use `doctype`
|
|
70
|
|
71 doctype 5
|
|
72 // <!DOCTYPE html>
|
|
73
|
|
74 Available options: `5`, `default`, `xml`, `transitional`, `strict`, `frameset`, `1.1`, `basic`, `mobile`
|
|
75
|
|
76 ### Tag Content
|
|
77
|
|
78 For single line tag text, you can just append the text after tag name:
|
|
79
|
|
80 p Testing!
|
|
81
|
|
82 would yield
|
|
83
|
|
84 <p>Testing!</p>
|
|
85
|
|
86 For 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
|
|
101 Input template data can be reached by key names directly. For example, assuming the template has been
|
|
102 executed 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
|
|
117 It is possible to interpolate fields using `#{}`
|
|
118
|
|
119 p Welcome #{Name}!
|
|
120
|
|
121 would print
|
|
122
|
|
123 ```html
|
|
124 <p>Welcome Ekin!</p>
|
|
125 ```
|
|
126
|
|
127 Attributes can have field names as well
|
|
128
|
|
129 a[title=Name][href="/ekin.koc"]
|
|
130
|
|
131 would print
|
|
132
|
|
133 ```html
|
|
134 <a title="Ekin" href="/ekin.koc"></a>
|
|
135 ```
|
|
136
|
|
137 ### Expressions
|
|
138
|
|
139 Amber can expand basic expressions. For example, it is possible to concatenate strings with + operator:
|
|
140
|
|
141 p Welcome #{Name + " " + LastName}
|
|
142
|
|
143 Arithmetic expressions are also supported:
|
|
144
|
|
145 p You need #{50 - Friends} more friends to reach 50!
|
|
146
|
|
147 Expressions can be used within attributes
|
|
148
|
|
149 img[alt=Name + " " + LastName][src=Avatar]
|
|
150
|
|
151 ### Variables
|
|
152
|
|
153 It is possible to define dynamic variables within templates,
|
|
154 all 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
|
|
160 If 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
|
|
166 For 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
|
|
176 Again, 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
|
|
182 There is a special syntax for conditional attributes. Only block attributes can have conditions;
|
|
183
|
|
184 div
|
|
185 .hasfriends ? Friends > 0
|
|
186
|
|
187 This would yield a div with `hasfriends` class only if the `Friends > 0` condition holds. It is
|
|
188 perfectly 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
|
|
196 It is possible to iterate over arrays and maps using `each`:
|
|
197
|
|
198 each $repo in Repositories
|
|
199 p #{$repo}
|
|
200
|
|
201 would print
|
|
202
|
|
203 p amber
|
|
204 p dateformat
|
|
205
|
|
206 It 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
|
|
215 Mixins (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
|
|
222 and 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
|
|
229 Template data, variables, expressions, etc., can all be passed as arguments:
|
|
230
|
|
231 +link(GoogleUrl, $googleTitle, "Check out " + $googleTitle)
|
|
232
|
|
233 ### Imports
|
|
234
|
|
235 A 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
|
|
248 gets compiled to
|
|
249
|
|
250 div
|
|
251 p this is template a
|
|
252 p this is template b
|
|
253
|
|
254 ### Inheritance
|
|
255
|
|
256 A template can inherit other templates. In order to inherit another template, an `extends` keyword should be used.
|
|
257 Parent 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
|
|
290 Copyright (c) 2012 Ekin Koc <ekin@eknkc.com>
|
|
291
|
|
292 Permission 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
|
|
294 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
295
|
|
296 THE 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
|
|
301 var DefaultOptions = Options{true, false}
|
|
302 var DefaultDirOptions = DirOptions{".amber", true}
|
|
303 ```
|
|
304
|
|
305 #### func Compile
|
|
306
|
|
307 ```go
|
|
308 func Compile(input string, options Options) (*template.Template, error)
|
|
309 ```
|
|
310 Parses and compiles the supplied amber template string. Returns corresponding Go
|
|
311 Template (html/templates) instance. Necessary runtime functions will be injected
|
|
312 and the template will be ready to be executed.
|
|
313
|
|
314 #### func CompileFile
|
|
315
|
|
316 ```go
|
|
317 func CompileFile(filename string, options Options) (*template.Template, error)
|
|
318 ```
|
|
319 Parses and compiles the contents of supplied filename. Returns corresponding Go
|
|
320 Template (html/templates) instance. Necessary runtime functions will be injected
|
|
321 and the template will be ready to be executed.
|
|
322
|
|
323 #### func CompileDir
|
|
324 ```go
|
|
325 func CompileDir(dirname string, dopt DirOptions, opt Options) (map[string]*template.Template, error)
|
|
326 ```
|
|
327 Parses 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
|
|
329 If there are templates in subdirectories, its key in the map will be it's path relative to `dirname`. For example:
|
|
330 ```
|
|
331 templates/
|
|
332 |-- index.amber
|
|
333 |-- layouts/
|
|
334 |-- base.amber
|
|
335 ```
|
|
336 ```go
|
|
337 templates, err := amber.CompileDir("templates/", amber.DefaultDirOptions, amber.DefaultOptions)
|
|
338 templates["index"] // index.amber Go Template
|
|
339 templates["layouts/base"] // base.amber Go Template
|
|
340 ```
|
|
341 By 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
|
|
346 type Compiler struct {
|
|
347 // Compiler options
|
|
348 Options
|
|
349 }
|
|
350 ```
|
|
351
|
|
352 Compiler is the main interface of Amber Template Engine. In order to use an
|
|
353 Amber template, it is required to create a Compiler and compile an Amber source
|
|
354 to 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
|
|
371 func New() *Compiler
|
|
372 ```
|
|
373 Create and initialize a new Compiler
|
|
374
|
|
375 #### func (*Compiler) Compile
|
|
376
|
|
377 ```go
|
|
378 func (c *Compiler) Compile() (*template.Template, error)
|
|
379 ```
|
|
380 Compile amber and create a Go Template (html/templates) instance. Necessary
|
|
381 runtime functions will be injected and the template will be ready to be
|
|
382 executed.
|
|
383
|
|
384 #### func (*Compiler) CompileString
|
|
385
|
|
386 ```go
|
|
387 func (c *Compiler) CompileString() (string, error)
|
|
388 ```
|
|
389 Compile template and return the Go Template source You would not be using this
|
|
390 unless debugging / checking the output. Please use Compile method to obtain a
|
|
391 template instance directly.
|
|
392
|
|
393 #### func (*Compiler) CompileWriter
|
|
394
|
|
395 ```go
|
|
396 func (c *Compiler) CompileWriter(out io.Writer) (err error)
|
|
397 ```
|
|
398 Compile amber and write the Go Template source into given io.Writer instance You
|
|
399 would not be using this unless debugging / checking the output. Please use
|
|
400 Compile method to obtain a template instance directly.
|
|
401
|
|
402 #### func (*Compiler) Parse
|
|
403
|
|
404 ```go
|
|
405 func (c *Compiler) Parse(input string) (err error)
|
|
406 ```
|
|
407 Parse given raw amber template string.
|
|
408
|
|
409 #### func (*Compiler) ParseFile
|
|
410
|
|
411 ```go
|
|
412 func (c *Compiler) ParseFile(filename string) (err error)
|
|
413 ```
|
|
414 Parse the amber template file in given path
|
|
415
|
|
416 #### type Options
|
|
417
|
|
418 ```go
|
|
419 type 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
|
|
436 type DirOptions struct {
|
|
437 // File extension to match for compilation
|
|
438 Ext string
|
|
439 // Whether or not to walk subdirectories
|
|
440 Recursive bool
|
|
441 }
|
|
442 ```
|