/scalate-website/src/documentation/_scaml-reference.md

http://github.com/scalate/scalate · Markdown · 1484 lines · 1231 code · 253 blank · 0 comment · 0 complexity · ff30dcd1cb2f8547be37ea76772a1001 MD5 · raw file

  1. # Scaml (Scala Markup Language)
  2. {:toc}
  3. Scaml is a very DRY way of writing XHTML templates. It is Scala
  4. version of [Haml](http://haml-lang.com/). Scaml functions as a replacement
  5. for inline page templating systems such as PHP, ERB, and ASP. However,
  6. Scaml avoids the need for explicitly coding XHTML into the template,
  7. because it uses a very concise white space active XHTML notation.
  8. ## Features
  9. * Whitespace active
  10. * Well-formatted markup
  11. * DRY
  12. * Follows CSS conventions
  13. * Integrates Scala code
  14. * [Haml](http://haml-lang.com/) or [Jade](http://jade-lang.com/) style
  15. notation
  16. ## Haml vs Jade Notation {#jade}
  17. Scaml supports both the original [Haml](http://haml-lang.com/) notation and a
  18. newer [Jade](http://jade-lang.com/) notation. The Jade dialect of Haml
  19. recognizes that Haml based notations are best used for rendering structured
  20. XHTML markup and that content is best rendered using something like markdown.
  21. It therefore, simplifies the element notation in exchange for complicating the
  22. plain content notation.
  23. {pygmentize_and_compare::}
  24. -----------------------------
  25. scaml: An example .scaml file
  26. -----------------------------
  27. %html
  28. %body
  29. The quick brown fox jumps
  30. over the lazy dog
  31. -----------------------------
  32. jade: An equivalent .jade file
  33. -----------------------------
  34. html
  35. body
  36. | The quick brown fox jumps
  37. | over the lazy dog
  38. {pygmentize_and_compare}
  39. Both examples above will render to the following:
  40. {pygmentize:: xml}
  41. <html>
  42. <body>
  43. The quick brown fox jumps
  44. over the lazy dog
  45. </body>
  46. </html>
  47. {pygmentize}
  48. The only difference between the Haml and Jade notation styles
  49. are that in the Jade notation style:
  50. * Elements do not get prefixed with `%`
  51. * Plain text sections must be prefixed with `|`
  52. The rest of this document will assume you are using the Haml notation
  53. style. All the examples will work with the Jade notation provided
  54. you apply the differences listed above.
  55. ## Plain Text
  56. A substantial portion of any HTML document is its content, which is plain old
  57. text. Any Scaml line that's not interpreted as something else is taken to be
  58. plain text, and passed through unmodified.
  59. For example:
  60. {pygmentize_and_compare::}
  61. -----------------------------
  62. scaml: example
  63. -----------------------------
  64. %gee
  65. %whiz
  66. Wow this is cool!
  67. -----------------------------
  68. xml: renders to
  69. -----------------------------
  70. <gee>
  71. <whiz>
  72. Wow this is cool!
  73. </whiz>
  74. </gee>
  75. {pygmentize_and_compare}
  76. Note that HTML tags are passed through unmodified as well.
  77. If you have some HTML you don't want to convert to Scaml,
  78. or you're converting a file line-by-line,
  79. you can just include it as-is.
  80. {pygmentize_and_compare::}
  81. -----------------------------
  82. scaml: example
  83. -----------------------------
  84. %p
  85. <div id="blah">Blah!</div>
  86. -----------------------------
  87. xml: renders to
  88. -----------------------------
  89. <p>
  90. <div id="blah">Blah!</div>
  91. </p>
  92. {pygmentize_and_compare}
  93. ### Escaping: `\`
  94. The backslash character escapes the first character of a line,
  95. allowing use of otherwise interpreted characters as plain text.
  96. {pygmentize_and_compare::}
  97. -----------------------------
  98. scaml: example
  99. -----------------------------
  100. %title
  101. = title
  102. \= title
  103. -----------------------------
  104. xml: renders to
  105. -----------------------------
  106. <title>
  107. MyPage
  108. = title
  109. </title>
  110. {pygmentize_and_compare}
  111. ## HTML Elements
  112. ### Element Name: `%`
  113. The percent character is placed at the beginning of a line.
  114. It's followed immediately by the name of an element,
  115. then optionally by modifiers (see below), a space,
  116. and text to be rendered inside the element.
  117. It creates an element in the form of `<element></element>`.
  118. {pygmentize_and_compare::}
  119. -----------------------------
  120. scaml: example
  121. -----------------------------
  122. %one
  123. %two
  124. %three Hey there
  125. -----------------------------
  126. xml: renders to
  127. -----------------------------
  128. <one>
  129. <two>
  130. <three>Hey there</three>
  131. </two>
  132. </one>
  133. {pygmentize_and_compare}
  134. Any string is a valid element name;
  135. Scaml will automatically generate opening and closing tags for any element.
  136. ### Attributes: `{` `}` or `(` `)` {#attributes}
  137. Brackets represent a Scala Map
  138. that is used for specifying the attributes of an element.
  139. Ruby hash syntax is used instead of Scala syntax to
  140. preserve a higher level of compatibility with the original
  141. Haml implementation.
  142. It is translated and evaluated as a Scala Map,
  143. so logic will work in it and local variables may be used.
  144. Quote characters within the attribute
  145. will be replaced by appropriate escape sequences.
  146. The hash is placed after the tag is defined.
  147. {pygmentize_and_compare::wide=true}
  148. -----------------------------
  149. scaml: example
  150. -----------------------------
  151. %html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}
  152. -----------------------------
  153. xml: renders to
  154. -----------------------------
  155. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"></html>
  156. {pygmentize_and_compare}
  157. Attribute hashes can also be stretched out over multiple lines
  158. to accommodate many attributes.
  159. However, newlines may only be placed immediately after commas.
  160. {pygmentize_and_compare::wide=true}
  161. -----------------------------
  162. scaml: example
  163. -----------------------------
  164. %script{:type => "text/javascript",
  165. :src => "javascripts/script"}
  166. -----------------------------
  167. xml: renders to
  168. -----------------------------
  169. <script type="text/javascript" src="javascripts/script"/>
  170. {pygmentize_and_compare}
  171. Complex expressions are supported if you wrap them between the `{`
  172. and `}` characters.
  173. {pygmentize_and_compare::}
  174. -----------------------------
  175. scaml: example
  176. -----------------------------
  177. %li{:counter={3+4}} Stuff
  178. -----------------------------
  179. xml: renders to
  180. -----------------------------
  181. <li counter="7">Stuff</li>
  182. {pygmentize_and_compare}
  183. #### HTML-style Attributes: `()`
  184. Scaml also supports a terser, less Scala-specific attribute syntax
  185. based on HTML's attributes.
  186. These are used with parentheses instead of brackets, like so:
  187. {pygmentize:: scaml}
  188. %html(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")
  189. {pygmentize}
  190. Scala variables can be used by omitting the quotes.
  191. For example:
  192. {pygmentize:: scaml}
  193. %a(title=title href=href) Stuff
  194. {pygmentize}
  195. This is the same as:
  196. {pygmentize:: scaml}
  197. %a{:title =>title, :href => href} Stuff
  198. {pygmentize}
  199. Complex expressions are supported if you wrap them between the `{`
  200. and `}` characters.
  201. {pygmentize_and_compare::}
  202. -----------------------------
  203. scaml: example
  204. -----------------------------
  205. %li(counter={3+4}) Stuff
  206. -----------------------------
  207. xml: renders to
  208. -----------------------------
  209. <li counter="7">Stuff</li>
  210. {pygmentize_and_compare}
  211. You can use both syntaxes together:
  212. {pygmentize:: scaml}
  213. %a(title="Hello"){:href => "http://scalate.fusesource.org"} Stuff
  214. {pygmentize}
  215. You can also use `#{}` interpolation to insert complicated expressions
  216. in a HTML-style attribute:
  217. {pygmentize:: scaml}
  218. %span(class="widget_#{widget.number}")
  219. {pygmentize}
  220. HTML-style attributes can be stretched across multiple lines
  221. just like hash-style attributes:
  222. {pygmentize:: scaml}
  223. %script(type="text/javascript"
  224. src="javascripts/script")
  225. {pygmentize}
  226. <!-- TODO
  227. #### Attribute Methods
  228. A Scala method call that returns a hash
  229. can be substituted for the hash contents.
  230. For example, Scaml::Helpers defines the following method:
  231. def html_attrs(lang = "en-US")
  232. {:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => lang, :lang => lang}
  233. end
  234. This can then be used in Scaml, like so:
  235. %html{html_attrs("fr-fr")}
  236. This is rendered to:
  237. <html lang="fr-fr" xml:lang="fr-fr" xmlns="http://www.w3.org/1999/xhtml">
  238. </html>
  239. You can use as many such attribute methods as you want
  240. by separating them with commas,
  241. like a Scala argument list.
  242. All the hashes will me merged together, from left to right.
  243. For example, if you defined
  244. def hash1
  245. {:bread => "white", :filling => "peanut butter and jelly"}
  246. end
  247. def hash2
  248. {:bread => "whole wheat"}
  249. end
  250. then
  251. %sandwich{hash1, hash2, :delicious => true}/
  252. would render to:
  253. <sandwich bread="whole wheat" delicious="true" filling="peanut butter and jelly" />
  254. Note that the Scaml attributes list has the same syntax as a Scala method call.
  255. This means that any attribute methods must come before the hash literal.
  256. Attribute methods aren't supported for HTML-style attributes.
  257. -->
  258. #### Boolean Attributes
  259. Some attributes, such as "checked" for `input` tags or "selected" for `option` tags,
  260. are "boolean" in the sense that their values don't matter -
  261. it only matters whether or not they're present.
  262. In HTML (but not XHTML), these attributes can be written as
  263. {pygmentize:: xml}
  264. <input selected>
  265. {pygmentize}
  266. To do this in Scaml using hash-style attributes, just assign a Scala
  267. `true` value to the attribute:
  268. {pygmentize:: scaml}
  269. %input{:selected => true}
  270. {pygmentize}
  271. In XHTML, the only valid value for these attributes is the name of the
  272. attribute. Thus this will render in XHTML as
  273. {pygmentize:: xml}
  274. <input selected="selected"/>
  275. {pygmentize}
  276. To set these attributes to false, simply assign them to a Scala false value.
  277. In both XHTML and HTML
  278. {pygmentize:: scaml}
  279. %input{:selected => false}
  280. {pygmentize}
  281. will just render as
  282. {pygmentize:: xml}
  283. <input/>
  284. {pygmentize}
  285. HTML-style boolean attributes can be written just like HTML:
  286. {pygmentize:: scaml}
  287. %input(selected)
  288. {pygmentize}
  289. or using `true` and `false`:
  290. {pygmentize:: scaml}
  291. %input(selected=true)
  292. {pygmentize}
  293. ### Class and ID: `.` and `#`
  294. The period and pound sign are borrowed from CSS.
  295. They are used as shortcuts to specify the `class`
  296. and `id` attributes of an element, respectively.
  297. Multiple class names can be specified in a similar way to CSS,
  298. by chaining the class names together with periods.
  299. They are placed immediately after the tag and before an attributes hash.
  300. {pygmentize_and_compare::}
  301. -----------------------------
  302. scaml: example
  303. -----------------------------
  304. %div#things
  305. %span#rice Chicken Fried
  306. %p.beans{ :food => "true" } The magical fruit
  307. %h1.class.otherclass#id La La La
  308. -----------------------------
  309. xml: renders to
  310. -----------------------------
  311. <div id="things">
  312. <span id="rice">Chicken Fried</span>
  313. <p class="beans" food="true">The magical fruit</p>
  314. <h1 id="id" class="class otherclass">La La La</h1>
  315. </div>
  316. {pygmentize_and_compare}
  317. And,
  318. {pygmentize_and_compare::}
  319. -----------------------------
  320. scaml: example
  321. -----------------------------
  322. #content
  323. .articles
  324. .article.title Doogie Howser Comes Out
  325. .article.date 2006-11-05
  326. .article.entry
  327. Neil Patrick Harris would like to dispel any rumors that he is straight
  328. -----------------------------
  329. xml: renders to
  330. -----------------------------
  331. <div id="content">
  332. <div class="articles">
  333. <div class="article title">Doogie Howser Comes Out</div>
  334. <div class="article date">2006-11-05</div>
  335. <div class="article entry">
  336. Neil Patrick Harris would like to dispel any rumors that he is straight
  337. </div>
  338. </div>
  339. </div>
  340. {pygmentize_and_compare}
  341. ### Weird Element Names: `'`element`'`
  342. Sometimes you have to generate markup with weird element names. Element names
  343. like `<funny.element/>`. Since Scaml interprets the period as a class name for the
  344. element, the following example:
  345. {pygmentize_and_compare::}
  346. -----------------------------
  347. scaml: example
  348. -----------------------------
  349. %funny.element
  350. -----------------------------
  351. xml: renders to
  352. -----------------------------
  353. <funny class="element"/>
  354. {pygmentize_and_compare}
  355. does not give you the desired result of `<funny.element/>`. In these cases you must single
  356. quote the element name.
  357. Example:
  358. {pygmentize_and_compare::}
  359. -----------------------------
  360. scaml: example
  361. -----------------------------
  362. %'funny.element'
  363. -----------------------------
  364. xml: renders to
  365. -----------------------------
  366. <funny.element/>
  367. {pygmentize_and_compare}
  368. #### Implicit Div Elements
  369. Because divs are used so often, they're the default elements.
  370. If you only define a class and/or id using `.` or `#`,
  371. a div is automatically used.
  372. {pygmentize_and_compare::}
  373. -----------------------------
  374. scaml: example
  375. -----------------------------
  376. #collection
  377. .item
  378. .description What a cool item!
  379. -----------------------------
  380. scaml: is the same as
  381. -----------------------------
  382. %div#collection
  383. %div.item
  384. %div.description What a cool item!
  385. {pygmentize_and_compare}
  386. and is rendered to:
  387. {pygmentize:: xml}
  388. <div id="collection">
  389. <div class="item">
  390. <div class="description">What a cool item!</div>
  391. </div>
  392. </div>
  393. {pygmentize}
  394. ### Self-Closing Tags: `/`
  395. The forward slash character, when placed at the end of a tag definition,
  396. causes the tag to be self-closed.
  397. For example:
  398. {pygmentize_and_compare::wide=true}
  399. -----------------------------
  400. scaml: example
  401. -----------------------------
  402. %br/
  403. %meta{"http-equiv" => "Content-Type", :content => "text/html"}/
  404. -----------------------------
  405. xml: renders to
  406. -----------------------------
  407. <br/>
  408. <meta http-equiv="Content-Type" content="text/html"/>
  409. {pygmentize_and_compare}
  410. Some tags are automatically closed, as long as they have no content.
  411. `meta`, `img`, `link`, `script`, `br`, and `hr` tags are closed by default.
  412. This list can be customized by setting the [`ScamlOptions.autoclose`](http://scalate.fusesource.org/maven/${project_version}/scalate-core/scaladocs/org/fusesource/scalate/scaml/ScamlOptions$.html) option.
  413. {pygmentize_and_compare::wide=true}
  414. -----------------------------
  415. scaml: example
  416. -----------------------------
  417. %br
  418. %meta{"http-equiv" => "Content-Type", :content => "text/html"}
  419. -----------------------------
  420. xml: renders to
  421. -----------------------------
  422. <br/>
  423. <meta http-equiv="Content-Type" content="text/html"/>
  424. {pygmentize_and_compare}
  425. ### Whitespace Removal: `>` and `<`
  426. `>` and `<` give you more control over the whitespace near a tag.
  427. `>` will remove all whitespace surrounding a tag,
  428. while `<` will remove all whitespace immediately within a tag.
  429. You can think of them as alligators eating the whitespace:
  430. `>` faces out of the tag and eats the whitespace on the outside,
  431. and `<` faces into the tag and eats the whitespace on the inside.
  432. They're placed at the end of a tag definition,
  433. after class, id, and attribute declarations
  434. but before `/` or `=`.
  435. {pygmentize_and_compare::}
  436. -----------------------------
  437. scaml: example
  438. -----------------------------
  439. %blockquote<
  440. %div
  441. Foo!
  442. -----------------------------
  443. xml: renders to
  444. -----------------------------
  445. <blockquote><div>
  446. Foo!
  447. </div></blockquote>
  448. {pygmentize_and_compare}
  449. And:
  450. {pygmentize_and_compare::}
  451. -----------------------------
  452. scaml: example
  453. -----------------------------
  454. %img
  455. %img>
  456. %img
  457. -----------------------------
  458. xml: renders to
  459. -----------------------------
  460. <img/><img/><img/>
  461. {pygmentize_and_compare}
  462. And:
  463. {pygmentize_and_compare::}
  464. -----------------------------
  465. scaml: example
  466. -----------------------------
  467. %p<= "Foo\nBar"
  468. -----------------------------
  469. xml: renders to
  470. -----------------------------
  471. <p>Foo
  472. Bar</p>
  473. {pygmentize_and_compare}
  474. And finally:
  475. {pygmentize_and_compare::}
  476. -----------------------------
  477. scaml: example
  478. -----------------------------
  479. %img
  480. %pre><
  481. foo
  482. bar
  483. %img
  484. -----------------------------
  485. xml: renders to
  486. -----------------------------
  487. <img /><pre>foo
  488. bar</pre><img />
  489. {pygmentize_and_compare}
  490. ## Doctype: `!!! format`
  491. When describing HTML documents with Scaml,
  492. you can have a document type or XML prolog generated automatically
  493. by including the characters `!!!`.
  494. {pygmentize_and_compare::wide=true}
  495. -----------------------------
  496. scaml: example
  497. -----------------------------
  498. !!! XML
  499. !!!
  500. %html
  501. %head
  502. %title Myspace
  503. %body
  504. %h1 I am the international space station
  505. %p Sign my guestbook
  506. -----------------------------
  507. xml: renders to
  508. -----------------------------
  509. <?xml version="1.0" encoding="utf-8" ?>
  510. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  511. <html>
  512. <head>
  513. <title>Myspace</title>
  514. </head>
  515. <body>
  516. <h1>I am the international space station</h1>
  517. <p>Sign my guestbook</p>
  518. </body>
  519. </html>
  520. {pygmentize_and_compare}
  521. You can also specify the specific doctype after the `!!!`
  522. When the `format` is set to `:xhtml` (the default),
  523. the following doctypes are supported:
  524. #### `!!!`
  525. > XHTML 1.0 Transitional
  526. > `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
  527. #### `!!! Strict`
  528. > XHTML 1.0 Strict
  529. > `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`
  530. #### `!!! Frameset`
  531. > XHTML 1.0 Frameset<br/>
  532. > `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">`
  533. #### `!!! 1.1`
  534. > XHTML 1.1<br/>
  535. > `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`
  536. #### `!!! Basic`
  537. > XHTML Basic 1.1<br/>
  538. > `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> `
  539. #### `!!! Mobile`
  540. > XHTML Mobile 1.2<br/>
  541. > `<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">`
  542. When the `format` option is set to `:html4`,
  543. the following doctypes are supported:
  544. #### `!!!`
  545. > HTML 4.01 Transitional<br/>
  546. > `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`
  547. #### `!!! Strict`
  548. > HTML 4.01 Strict<br/>
  549. > `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">`
  550. #### `!!! Frameset`
  551. > HTML 4.01 Frameset<br/>
  552. > `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">`
  553. #### `!!! 5`
  554. > HTML 5<br/>
  555. > `<!DOCTYPE html>`<br/>
  556. When the `format` option is set to `:html5`,
  557. `!!!` is always `<!DOCTYPE html>`.
  558. If you're not using the UTF-8 character set for your document,
  559. you can specify which encoding should appear
  560. in the XML prolog in a similar way.
  561. {pygmentize_and_compare::}
  562. -----------------------------
  563. scaml: example
  564. -----------------------------
  565. !!! XML iso-8859-1
  566. -----------------------------
  567. xml: renders to
  568. -----------------------------
  569. <?xml version="1.0" encoding="iso-8859-1" ?>
  570. {pygmentize_and_compare}
  571. ## Comments
  572. Scaml supports two sorts of comments:
  573. those that show up in the HTML output
  574. and those that don't.
  575. ### HTML Comments: `/`
  576. The forward slash character, when placed at the beginning of a line,
  577. wraps all text after it in an HTML comment.
  578. {pygmentize_and_compare::}
  579. -----------------------------
  580. scaml: example
  581. -----------------------------
  582. %peanutbutterjelly
  583. / This is the comment
  584. I like sandwiches!
  585. -----------------------------
  586. xml: renders to
  587. -----------------------------
  588. <peanutbutterjelly>
  589. <!-- This is the comment -->
  590. I like sandwiches!
  591. </peanutbutterjelly>
  592. {pygmentize_and_compare}
  593. The forward slash can also wrap indented sections of code. For example:
  594. {pygmentize_and_compare::}
  595. -----------------------------
  596. scaml: example
  597. -----------------------------
  598. /
  599. %p This doesn't render...
  600. %div
  601. %h1 Because it's commented out!
  602. -----------------------------
  603. xml: renders to
  604. -----------------------------
  605. <!--
  606. <p>This doesn't render...</p>
  607. <div>
  608. <h1>Because it's commented out!</h1>
  609. </div>
  610. -->
  611. {pygmentize_and_compare}
  612. #### Conditional Comments: `/[]`
  613. You can also use [Internet Explorer conditional comments](http://www.quirksmode.org/css/condcom.html)
  614. by enclosing the condition in square brackets after the `/`.
  615. {pygmentize_and_compare::wide=true}
  616. -----------------------------
  617. scaml: example
  618. -----------------------------
  619. /[if IE]
  620. %a{ :href => "http://www.mozilla.com/en-US/firefox/" }
  621. %h1 Get Firefox
  622. -----------------------------
  623. xml: renders to
  624. -----------------------------
  625. <!--[if IE]>
  626. <a href="http://www.mozilla.com/en-US/firefox/">
  627. <h1>Get Firefox</h1>
  628. </a>
  629. <![endif]-->
  630. {pygmentize_and_compare}
  631. ### Scaml Comments: `-#`
  632. The hyphen followed immediately by the pound sign
  633. signifies a silent comment.
  634. Any text following this isn't rendered in the resulting document
  635. at all.
  636. For example:
  637. {pygmentize_and_compare::}
  638. -----------------------------
  639. scaml: example
  640. -----------------------------
  641. %p foo
  642. -# This is a comment
  643. %p bar
  644. -----------------------------
  645. xml: renders to
  646. -----------------------------
  647. <p>foo</p>
  648. <p>bar</p>
  649. {pygmentize_and_compare}
  650. You can also nest text beneath a silent comment.
  651. None of this text will be rendered.
  652. {pygmentize_and_compare::}
  653. -----------------------------
  654. scaml: example
  655. -----------------------------
  656. %p foo
  657. -#
  658. This won't be displayed
  659. Nor will this
  660. %p bar
  661. -----------------------------
  662. xml: renders to
  663. -----------------------------
  664. <p>foo</p>
  665. <p>bar</p>
  666. {pygmentize_and_compare}
  667. ## Scala Evaluation
  668. ### Binding Attributes `-@` {#bindings}
  669. When a Scalate template is rendered, the caller can pass an attribute map
  670. to the template in charge of rendering. To bind the attribute to a Scala
  671. variable, a Scaml template uses the hyphen character followed by an at sign
  672. and then a Scala variable declaration statement.
  673. For example To define an attribute use the following declaration
  674. {pygmentize:: scaml}
  675. -@ val foo: MyType
  676. {pygmentize}
  677. If the attribute map does not contain a "foo" entry, then a
  678. NoValueSetException is thrown when the the template is rendered.
  679. To avoid this exception, a default value can be configured. For
  680. example:
  681. {pygmentize:: scaml}
  682. -@ val bar: String = "this is the default value"
  683. {pygmentize}
  684. The attribute is now available for use as an expression.
  685. Its very common to have a template based on a single object who's members are
  686. frequently accessed. In these cases, it's convenient to import all the object's
  687. members. This can be done by adding the import keyword to the attribute declaration.
  688. For example:
  689. {pygmentize:: scaml}
  690. -@ import val model: Person
  691. %p Hello #{name}, what is the weather like in #{city}
  692. {pygmentize}
  693. is the same as:
  694. {pygmentize:: scaml}
  695. -@ val model: Person
  696. - import model._
  697. %p Hello #{name}, what is the weather like in #{city}
  698. {pygmentize}
  699. Which is the same as:
  700. {pygmentize:: scaml}
  701. -@ val model: Person
  702. %p Hello #{model.name}, what is the weather like in #{model.city}
  703. {pygmentize}
  704. ### Inserting Scala: `=`
  705. The equals character is followed by Scala code.
  706. This code is evaluated and the output is inserted into the document.
  707. {pygmentize_and_compare::wide=true}
  708. -----------------------------
  709. scaml: example
  710. -----------------------------
  711. %p
  712. = List("hi", "there", "reader!").mkString(" ")
  713. = "yo"
  714. -----------------------------
  715. xml: renders to
  716. -----------------------------
  717. <p>
  718. hi there reader!
  719. yo
  720. </p>
  721. {pygmentize_and_compare}
  722. The default setting for the [`TemplateEngine.escapeMarkup`](http://scalate.fusesource.org/maven/${project_version}/scalate-core/scaladocs/org/fusesource/scalate/TemplateEngine.html) option is
  723. true. When `TemplateEngine.escapeMarkup` is enabled, `=` will sanitize any
  724. HTML-sensitive characters generated by the script.
  725. {pygmentize_and_compare::wide=true}
  726. -----------------------------
  727. scaml: example
  728. -----------------------------
  729. = """<script>alert("I'm evil!");</script>"""
  730. -----------------------------
  731. xml: renders to
  732. -----------------------------
  733. &lt;script&gt;alert(&quot;I'm evil!&quot;);&lt;/script&gt;
  734. {pygmentize_and_compare}
  735. `=` can also be used at the end of a tag to insert Scala code within that tag.
  736. {pygmentize_and_compare::}
  737. -----------------------------
  738. scaml: example
  739. -----------------------------
  740. %p= "hello"
  741. -----------------------------
  742. xml: renders to
  743. -----------------------------
  744. <p>hello</p>
  745. {pygmentize_and_compare}
  746. ### Running Scala: `-`
  747. The hyphen character is also followed by Scala code.
  748. This code is evaluated but *not* inserted into the document.
  749. **It is not recommended that you use this widely;
  750. almost all processing code and logic should be restricted
  751. to the Controller, the Helper, or partials.**
  752. {pygmentize_and_compare::}
  753. -----------------------------
  754. scaml: example
  755. -----------------------------
  756. - var foo = "hello"
  757. - foo += " there"
  758. - foo += " you!"
  759. %p= foo
  760. -----------------------------
  761. xml: renders to
  762. -----------------------------
  763. <p>hello there you!</p>
  764. {pygmentize_and_compare}
  765. Or alternatively, if you have a large block of Scala code, you can
  766. nest it under the hyphen character as demonstrated by the following example:
  767. {pygmentize_and_compare::}
  768. -----------------------------
  769. scaml: example
  770. -----------------------------
  771. -
  772. var foo = "hello"
  773. foo += " there"
  774. foo += " you!"
  775. %p= foo
  776. -----------------------------
  777. xml: renders to
  778. -----------------------------
  779. <p>hello there you!</p>
  780. {pygmentize_and_compare}
  781. #### Scala Blocks
  782. Scala blocks, like XHTML tags, don't need to be explicitly closed in Scaml.
  783. Rather, they're automatically closed, based on indentation.
  784. A block begins whenever the indentation is increased
  785. after a Scala insertion or evaluation command.
  786. It ends when the indentation decreases.
  787. {pygmentize_and_compare::}
  788. -----------------------------
  789. scaml: example
  790. -----------------------------
  791. - for(i <- 42 to 46)
  792. %p= i
  793. %p See, I can count!
  794. -----------------------------
  795. xml: renders to
  796. -----------------------------
  797. <p>42</p>
  798. <p>43</p>
  799. <p>44</p>
  800. <p>45</p>
  801. <p>46</p>
  802. <p>See, I can count!</p>
  803. {pygmentize_and_compare}
  804. And,
  805. {pygmentize_and_compare::}
  806. -----------------------------
  807. scaml: example
  808. -----------------------------
  809. %p
  810. - 2 match
  811. - case 1 =>
  812. = "one"
  813. - case 2 =>
  814. = "two"
  815. - case 3 =>
  816. = "three"
  817. -----------------------------
  818. xml: renders to
  819. -----------------------------
  820. <p>
  821. two
  822. </p>
  823. {pygmentize_and_compare}
  824. When inserting evaluated statements, it can also take advantage of Scala blocks. It can be handy
  825. for passing partial functions.
  826. For example:
  827. {pygmentize:: scaml}
  828. %p
  829. = List(1,2,3).foldLeft("result: ")
  830. - (a,x)=>
  831. - a+x
  832. {pygmentize}
  833. is the same as:
  834. {pygmentize:: scaml}
  835. %p
  836. = List(1,2,3).foldLeft("result: ") { (a,x)=> { a+x } }
  837. {pygmentize}
  838. would be rendered to:
  839. {pygmentize:: xml}
  840. <p>
  841. result: 123
  842. </p>
  843. {pygmentize}
  844. ### Whitespace Preservation: `~` {#tilde}
  845. `~` works just like `=`, except that it preserves the white space
  846. formating on its input.
  847. Scaml always produces HTML source which is easy to read since
  848. it properly indented. Even dynamically generated output is
  849. properly indented.
  850. {pygmentize_and_compare::}
  851. -----------------------------
  852. scaml: example
  853. -----------------------------
  854. %html
  855. %p
  856. = "line1\nline2\nline3"
  857. -----------------------------
  858. xml: renders to
  859. -----------------------------
  860. <html>
  861. <p>
  862. line1
  863. line2
  864. line3
  865. </p>
  866. </html>
  867. {pygmentize_and_compare}
  868. Sometimes you don't want Scaml to indent the dynamically generated content.
  869. For example, tags like `pre` and `textarea` are whitespace-sensitive;
  870. indenting the text makes them render wrong.
  871. When you use `~` instead of `=`,
  872. Scaml will convert newlines to the XHTML newline escape code, `&#x000A;` and avoid
  873. adding spaces for indentation.
  874. {pygmentize_and_compare::}
  875. -----------------------------
  876. scaml: example
  877. -----------------------------
  878. %html
  879. %pre
  880. ~ "line1\nline2\nline3"
  881. -----------------------------
  882. xml: renders to
  883. -----------------------------
  884. <html>
  885. <pre>
  886. line1&#x000A;line2&#x000A;line3
  887. </pre>
  888. </html>
  889. {pygmentize_and_compare}
  890. #### Ugly Preservation: `~~` {#tilde-tilde}
  891. Sometimes, you don't want Scaml to indent or apply the whitespace transformation on
  892. the evaluated expression. When this is the case, use `~~` to use ugly whitespace
  893. preservation. We call it ugly because the produce HTML will not properly indented.
  894. {pygmentize_and_compare::}
  895. -----------------------------
  896. scaml: example
  897. -----------------------------
  898. %html
  899. %p
  900. ~~ "line1\nline2\nline3"
  901. -----------------------------
  902. xml: renders to
  903. -----------------------------
  904. <html>
  905. <p>
  906. line1
  907. line2
  908. line3
  909. </p>
  910. </html>
  911. {pygmentize_and_compare}
  912. ### Scala Interpolation: `#{}`
  913. Scala code can be interpolated within plain text using `#{}`.
  914. {pygmentize_and_compare::}
  915. -----------------------------
  916. scaml: example
  917. -----------------------------
  918. %p This is #{quality} cake!
  919. -----------------------------
  920. xml: is the same as
  921. -----------------------------
  922. %p= "This is "+(quality)+" cake!"
  923. {pygmentize_and_compare}
  924. and renders to
  925. {pygmentize:: xml}
  926. <p>This is scrumptious cake!</p>
  927. {pygmentize}
  928. Backslashes can be used to escape `#{` strings,
  929. but they don't act as escapes anywhere else in the string.
  930. {pygmentize_and_compare::wide=true}
  931. -----------------------------
  932. scaml: example
  933. -----------------------------
  934. %p
  935. A slash make a difference here: \#{name} is set to: \\#{name}
  936. But is ignored for: \# or \\
  937. -----------------------------
  938. xml: renders to
  939. -----------------------------
  940. <p>
  941. A slash make a difference here: #{name} is set to: \Hiram
  942. But is ignored for: \# or \\
  943. </p>
  944. {pygmentize_and_compare}
  945. <!--
  946. Interpolation can also be used within [filters](#filters).
  947. For example:
  948. :javascript
  949. $(document).ready(function() {
  950. alert(#{message.to_json});
  951. });
  952. might compile to
  953. <script type="text/javascript">
  954. //<![CDATA[
  955. $(document).ready(function() {
  956. alert("Hi there!");
  957. });
  958. //]]>
  959. </script>
  960. -->
  961. ### Escaping HTML: `&=` {#escaping_html}
  962. An ampersand followed by one or two equals characters
  963. evaluates Scala code just like the equals without the ampersand,
  964. but sanitizes any HTML-sensitive characters in the result of the code.
  965. {pygmentize_and_compare::}
  966. -----------------------------
  967. scaml: example
  968. -----------------------------
  969. &= "I like cheese & crackers"
  970. -----------------------------
  971. xml: renders to
  972. -----------------------------
  973. I like cheese &amp; crackers
  974. {pygmentize_and_compare}
  975. When the [`TemplateEngine.escapeMarkup`](http://scalate.fusesource.org/maven/${project_version}/scalate-core/scaladocs/org/fusesource/scalate/TemplateEngine.html) option is set
  976. to true, `=` behaves identically to `&=`.
  977. `&` can also be used on its own so that `#{}` interpolation is escaped.
  978. {pygmentize_and_compare::}
  979. -----------------------------
  980. scaml: example
  981. -----------------------------
  982. & I like #{"cheese & crackers"}
  983. -----------------------------
  984. xml: renders to
  985. -----------------------------
  986. I like cheese &amp; crackers
  987. {pygmentize_and_compare}
  988. ### Unescaping HTML: `!=` {#unescaping_html}
  989. An exclamation mark followed by one or two equals characters
  990. evaluates Scala code just like the equals would,
  991. but never sanitizes the HTML.
  992. When the [`TemplateEngine.escapeMarkup`](http://scalate.fusesource.org/maven/${project_version}/scalate-core/scaladocs/org/fusesource/scalate/TemplateEngine.html) option is set to false, `=` behaves identically to `!=`.
  993. However, if the [`TemplateEngine.escapeMarkup`](http://scalate.fusesource.org/maven/${project_version}/scalate-core/scaladocs/org/fusesource/scalate/TemplateEngine.html) option is set to true, `=` will sanitize the HTML, but `!=` still won't.
  994. For example, if `TemplateEngine.escapeMarkup` is true:
  995. {pygmentize_and_compare::}
  996. -----------------------------
  997. scaml: example
  998. -----------------------------
  999. = "I feel <strong>!"
  1000. != "I feel <strong>!"
  1001. -----------------------------
  1002. xml: renders to
  1003. -----------------------------
  1004. I feel &lt;strong&gt;!
  1005. I feel <strong>!
  1006. {pygmentize_and_compare}
  1007. `!` can also be used on its own so that `#{}` interpolation is unescaped.
  1008. {pygmentize_and_compare::}
  1009. -----------------------------
  1010. scaml: example
  1011. -----------------------------
  1012. ! I feel #{"<strong>"}!
  1013. -----------------------------
  1014. xml: renders to
  1015. -----------------------------
  1016. I feel <strong>!
  1017. {pygmentize_and_compare}
  1018. ## Filters: `:` {#filters}
  1019. The colon character designates a filter.
  1020. This allows you to pass an indented block of text as input
  1021. to another filtering program and add the result to the output of Haml.
  1022. The syntax is a colon followed by an optional list of filter flags and then a colon
  1023. separated list of filter names.
  1024. {pygmentize_and_compare::}
  1025. -----------------------------
  1026. scaml: example
  1027. -----------------------------
  1028. %p
  1029. :markdown
  1030. Markdown
  1031. ========
  1032. Hello, *World*
  1033. -----------------------------
  1034. xml: renders to
  1035. -----------------------------
  1036. <p>
  1037. <h1>Markdown</h1>
  1038. <p>Hello, <em>World</em></p>
  1039. </p>
  1040. {pygmentize_and_compare}
  1041. ### Filter Interpolation
  1042. If you use the `!` or `&` filter flags, you can have Scala code
  1043. interpolated with `#{}` expressions. It is invalid to use both
  1044. the `!` and `&` flags at the same time.
  1045. The `&` flag enables sanitized interpolations.
  1046. {pygmentize_and_compare::wide=true}
  1047. -----------------------------
  1048. scaml: example
  1049. -----------------------------
  1050. - var flavor = "<raspberry/>"
  1051. #content
  1052. :&markdown
  1053. I *really* prefer #{flavor} jam.
  1054. -----------------------------
  1055. xml: renders to
  1056. -----------------------------
  1057. <div id="content">
  1058. <p>I <em>really</em> prefer &lt;raspberry/&gt; jam.</p>
  1059. </div>
  1060. {pygmentize_and_compare}
  1061. The `!` flag enables non-sanitized interpolations.
  1062. {pygmentize_and_compare::wide=true}
  1063. -----------------------------
  1064. scaml: example
  1065. -----------------------------
  1066. - var flavor = "<raspberry/>"
  1067. #content
  1068. :!markdown
  1069. I *really* prefer #{flavor} jam.
  1070. -----------------------------
  1071. xml: renders to
  1072. -----------------------------
  1073. <div id="content">
  1074. <p>I <em>really</em> prefer <raspberry/>; jam.</p>
  1075. </div>
  1076. {pygmentize_and_compare}
  1077. ### Filter Whitespace Preservation
  1078. The `~` filter flag enables preserves the white space of the content.
  1079. The indent level is left unchanged and newlines are converted to `&#x000A;`
  1080. {pygmentize_and_compare::wide=true}
  1081. -----------------------------
  1082. scaml: example
  1083. -----------------------------
  1084. %html
  1085. %p<
  1086. :~plain
  1087. Indentation levels are not enforced in filters.
  1088. #{Interpolation} is disabled by default
  1089. Last line
  1090. -----------------------------
  1091. xml: renders to
  1092. -----------------------------
  1093. <html>
  1094. <p> Indentation levels are not enforced in filters.&#x000A; #{Interpolation} is disabled by default&#x000A;Last line</p>
  1095. </html>
  1096. {pygmentize_and_compare}
  1097. ### Filter Chaining
  1098. More than one filter can be be used by separating each filter name with a colon. When
  1099. this is done, the filters are chained together so that the output of filter on right, is
  1100. passed as input to the filter on the left.
  1101. {pygmentize_and_compare::}
  1102. -----------------------------
  1103. scaml: example
  1104. -----------------------------
  1105. %pre
  1106. :escaped :javascript
  1107. alert("Hello");
  1108. -----------------------------
  1109. xml: renders to
  1110. -----------------------------
  1111. <pre>
  1112. &lt;script type='text/javascript'&gt;
  1113. //&lt;![CDATA[
  1114. alert(&quot;Hello&quot;);
  1115. //]]&gt;
  1116. &lt;/script&gt;
  1117. </pre>
  1118. {pygmentize_and_compare}
  1119. ### Available Filters
  1120. Scaml has the following filters defined:
  1121. #### `:plain` {#plain-filter}
  1122. > Does not parse the filtered text.
  1123. > This is useful for large blocks of text or HTML. Really handy when
  1124. > when you don't want lines starting with `.` or `-` to be parsed.
  1125. #### `:javascript` {#javascript-filter}
  1126. > Surrounds the filtered text with `<script>` and CDATA tags.
  1127. > Useful for including inline Javascript.
  1128. #### `:css` {#css-filter}
  1129. > Surrounds the filtered text with `<style>` and CDATA tags.
  1130. Useful for including inline CSS.
  1131. #### `:cdata` {#cdata-filter}
  1132. > Surrounds the filtered text with CDATA tags.
  1133. #### `:escaped` {#escaped-filter}
  1134. > Works the same as plain, but HTML-escapes the text
  1135. > before placing it in the document.
  1136. <!--
  1137. #### `:ruby` {#ruby-filter}
  1138. Parses the filtered text with the normal Ruby interpreter.
  1139. All output sent to `$stdout`, like with `puts`,
  1140. is output into the Haml document.
  1141. Not available if the [`:suppress_eval`](#suppress_eval-option) option is set to true.
  1142. The Ruby code is evaluated in the same context as the Haml template.
  1143. #### `:erb` {#erb-filter}
  1144. Parses the filtered text with ERb, like an RHTML template.
  1145. Not available if the [`:suppress_eval`](#suppress_eval-option) option is set to true.
  1146. Embedded Ruby code is evaluated in the same context as the Haml template.
  1147. -->
  1148. #### `:sass` {#sass-filter}
  1149. > Parses the filtered text with [Sass](http://sass-lang.com/) to produce CSS output.
  1150. Only works if you have the `scalate-jruby` module on the class path. You normally
  1151. want to combine with the `:css` filter. For example `:css:sass`
  1152. #### `:scss` {#scss-filter}
  1153. > Parses the filtered text with [Scss](http://sass-lang.com/) to produce CSS output.
  1154. Only works if you have the `scalate-jruby` module on the class path. You normally
  1155. want to combine with the `:css` filter. For example `:css:scss`
  1156. #### `:textile` {#textile-filter}
  1157. > Parses the filtered text with [Textile](http://www.textism.com/tools/textile).
  1158. Only works the scalate-wikitext module is found on the class path.
  1159. #### `:markdown` {#markdown-filter}
  1160. > Parses the filtered text with [Markdown](http://daringfireball.net/projects/markdown).
  1161. Only works if [scalamd](http://scalamd.fusesource.org/) or [MarkdownJ](http://markdownj.org/) is found on the class path.
  1162. <!--
  1163. ### Custom Filters
  1164. You can also define your own filters. See Haml::Filters for details.
  1165. -->
  1166. ## Global Scaml Options
  1167. There are several global options you can configure to customize how Scaml renders the
  1168. output. You will need to configure these before any of your scaml templates are compiled
  1169. as they affect the generated scala template classes.
  1170. ### `ScamlOptions.indent`
  1171. The `ScamlOptions.indent` option is used to control what kind of indenting characters to
  1172. use in the rendered markup. It defaults to two spaces but can be set to the tab character
  1173. or set to the empty string to disable indenting alltogether.
  1174. {pygmentize_and_compare::}
  1175. -----------------------------
  1176. scaml: ScamlOptions.indent = ""
  1177. -----------------------------
  1178. %gee
  1179. %whiz
  1180. Wow this is cool!
  1181. -----------------------------
  1182. xml: renders to
  1183. -----------------------------
  1184. <gee>
  1185. <whiz>
  1186. Wow this is cool!
  1187. </whiz>
  1188. </gee>
  1189. {pygmentize_and_compare}
  1190. ### `ScamlOptions.nl`
  1191. The `ScamlOptions.nl` option is used to control what kind of new line seperator to
  1192. use in the rendered markup. It defaults to `\n`. Some folks may want to set it and the indent
  1193. to the empty string to reduce the generated document sizes.
  1194. For example, if `ScamlOptions.indent = ""` and `ScamlOptions.nl = ""` then:
  1195. {pygmentize_and_compare::}
  1196. -----------------------------
  1197. scaml: example
  1198. -----------------------------
  1199. %gee
  1200. %whiz
  1201. Wow this is cool!
  1202. -----------------------------
  1203. xml: renders to
  1204. -----------------------------
  1205. <gee><whiz>Wow this is cool!</whiz></gee>
  1206. {pygmentize_and_compare}
  1207. ### `ScamlOptions.ugly`
  1208. Enabling the `ScamlOptions.ugly` option makes `=` statements work like `~~` statements. The dynamic expressions
  1209. will not be indented and they the new line preservation transformation method will not be applied. Enabling the
  1210. ugly option will significantly reduce the CPU overhead of processing dynamic expressions.
  1211. {pygmentize_and_compare::}
  1212. -----------------------------
  1213. scaml: ScamlOptions.ugly = true
  1214. -----------------------------
  1215. %html
  1216. %p
  1217. = "line1\nline2\nline3"
  1218. -----------------------------
  1219. xml: renders to
  1220. -----------------------------
  1221. <html>
  1222. <p>
  1223. line1
  1224. line2
  1225. line3
  1226. </p>
  1227. </html>
  1228. {pygmentize_and_compare}
  1229. ## Other Resources
  1230. * [User Guide](user-guide.html)
  1231. * [Documentation](index.html)