You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

258 lines
6.4 KiB

  1. /*
  2. Package amber is an elegant templating engine for Go Programming Language.
  3. It is inspired from HAML and Jade.
  4. Tags
  5. A tag is simply a word:
  6. html
  7. is converted to
  8. <html></html>
  9. It is possible to add ID and CLASS attributes to tags:
  10. div#main
  11. span.time
  12. are converted to
  13. <div id="main"></div>
  14. <span class="time"></span>
  15. Any arbitrary attribute name / value pair can be added this way:
  16. a[href="http://www.google.com"]
  17. You can mix multiple attributes together
  18. a#someid[href="/"][title="Main Page"].main.link Click Link
  19. gets converted to
  20. <a id="someid" class="main link" href="/" title="Main Page">Click Link</a>
  21. It is also possible to define these attributes within the block of a tag
  22. a
  23. #someid
  24. [href="/"]
  25. [title="Main Page"]
  26. .main
  27. .link
  28. | Click Link
  29. Doctypes
  30. To add a doctype, use `!!!` or `doctype` keywords:
  31. !!! transitional
  32. // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  33. or use `doctype`
  34. doctype 5
  35. // <!DOCTYPE html>
  36. Available options: `5`, `default`, `xml`, `transitional`, `strict`, `frameset`, `1.1`, `basic`, `mobile`
  37. Tag Content
  38. For single line tag text, you can just append the text after tag name:
  39. p Testing!
  40. would yield
  41. <p>Testing!</p>
  42. For multi line tag text, or nested tags, use indentation:
  43. html
  44. head
  45. title Page Title
  46. body
  47. div#content
  48. p
  49. | This is a long page content
  50. | These lines are all part of the parent p
  51. a[href="/"] Go To Main Page
  52. Data
  53. Input template data can be reached by key names directly. For example, assuming the template has been
  54. executed with following JSON data:
  55. {
  56. "Name": "Ekin",
  57. "LastName": "Koc",
  58. "Repositories": [
  59. "amber",
  60. "dateformat"
  61. ],
  62. "Avatar": "/images/ekin.jpg",
  63. "Friends": 17
  64. }
  65. It is possible to interpolate fields using `#{}`
  66. p Welcome #{Name}!
  67. would print
  68. <p>Welcome Ekin!</p>
  69. Attributes can have field names as well
  70. a[title=Name][href="/ekin.koc"]
  71. would print
  72. <a title="Ekin" href="/ekin.koc"></a>
  73. Expressions
  74. Amber can expand basic expressions. For example, it is possible to concatenate strings with + operator:
  75. p Welcome #{Name + " " + LastName}
  76. Arithmetic expressions are also supported:
  77. p You need #{50 - Friends} more friends to reach 50!
  78. Expressions can be used within attributes
  79. img[alt=Name + " " + LastName][src=Avatar]
  80. Variables
  81. It is possible to define dynamic variables within templates,
  82. all variables must start with a $ character and can be assigned as in the following example:
  83. div
  84. $fullname = Name + " " + LastName
  85. p Welcome #{$fullname}
  86. If you need to access the supplied data itself (i.e. the object containing Name, LastName etc fields.) you can use `$` variable
  87. p $.Name
  88. Conditions
  89. For conditional blocks, it is possible to use `if <expression>`
  90. div
  91. if Friends > 10
  92. p You have more than 10 friends
  93. else if Friends > 5
  94. p You have more than 5 friends
  95. else
  96. p You need more friends
  97. Again, it is possible to use arithmetic and boolean operators
  98. div
  99. if Name == "Ekin" && LastName == "Koc"
  100. p Hey! I know you..
  101. There is a special syntax for conditional attributes. Only block attributes can have conditions;
  102. div
  103. .hasfriends ? Friends > 0
  104. This would yield a div with `hasfriends` class only if the `Friends > 0` condition holds. It is
  105. perfectly fine to use the same method for other types of attributes:
  106. div
  107. #foo ? Name == "Ekin"
  108. [bar=baz] ? len(Repositories) > 0
  109. Iterations
  110. It is possible to iterate over arrays and maps using `each`:
  111. each $repo in Repositories
  112. p #{$repo}
  113. would print
  114. p amber
  115. p dateformat
  116. It is also possible to iterate over values and indexes at the same time
  117. each $i, $repo in Repositories
  118. p
  119. .even ? $i % 2 == 0
  120. .odd ? $i % 2 == 1
  121. Includes
  122. A template can include other templates using `include`:
  123. a.amber
  124. p this is template a
  125. b.amber
  126. p this is template b
  127. c.amber
  128. div
  129. include a
  130. include b
  131. gets compiled to
  132. div
  133. p this is template a
  134. p this is template b
  135. Inheritance
  136. A template can inherit other templates. In order to inherit another template, an `extends` keyword should be used.
  137. Parent template can define several named blocks and child template can modify the blocks.
  138. master.amber
  139. !!! 5
  140. html
  141. head
  142. block meta
  143. meta[name="description"][content="This is a great website"]
  144. title
  145. block title
  146. | Default title
  147. body
  148. block content
  149. subpage.amber
  150. extends master
  151. block title
  152. | Some sub page!
  153. block append meta
  154. // This will be added after the description meta tag. It is also possible
  155. // to prepend something to an existing block
  156. meta[name="keywords"][content="foo bar"]
  157. block content
  158. div#main
  159. p Some content here
  160. License
  161. (The MIT License)
  162. Copyright (c) 2012 Ekin Koc <ekin@eknkc.com>
  163. 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:
  164. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  165. 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.
  166. */
  167. package amber