/scalate-website/src/documentation/scuery.page

http://github.com/scalate/scalate · Visualforce Page · 126 lines · 100 code · 26 blank · 0 comment · 0 complexity · 9b5c22e80dc1afa3d9745f1f15aa2f6d MD5 · raw file

  1. ---
  2. title: Scuery
  3. in_menu: false
  4. sort_info: 2
  5. --- name:overview
  6. # Scuery
  7. Transform HTML using jQuery style DSL in Scala
  8. --- name:content pipeline:jade
  9. .left
  10. :markdown
  11. # Scuery
  12. .right
  13. :markdown
  14. **Scuery** provides an XML transformation library which uses a jQuery style approach, using CSS3 selectors to transform HTML or XHTML to inject dynamic information from static HTML/XHTML documents owned by designers.
  15. For example a designer could create a mock up layout in HTML; or have a sample page with mock data. Scuery can then be used to transform the XML, using Scala's [NodeSeq](http://www.scala-lang.org/api/current/scala/xml/NodeSeq.html) with CSS3 selectors.
  16. .left
  17. :markdown
  18. # Simple Example
  19. .right
  20. :markdown
  21. If you had the following HTML...
  22. {pygmentize:: html}
  23. <div id="content">
  24. <table class="people">
  25. <tr>
  26. <th>Name</th>
  27. <th>Location</th>
  28. </tr>
  29. <tr>
  30. <td class="name">DummyName</td>
  31. <td class="location">DummyLocation</td>
  32. </tr>
  33. </table>
  34. </div>
  35. {pygmentize}
  36. Then you can transform this with the following code.
  37. {pygmentize:: scala}
  38. object transformer extends Transformer {
  39. $("table .name").contents = "Hiram"
  40. $(".location").contents = "Tampa"
  41. }
  42. val result = transformer(xml)
  43. {pygmentize}
  44. We are creating an object, transformer, which we can then use as a function to transform XML.
  45. .left
  46. :markdown
  47. # More complex example
  48. .right
  49. :markdown
  50. Typically you want to iterate through collections so something like this...
  51. {pygmentize:: html}
  52. <div id="content">
  53. <table class="people">
  54. <tr>
  55. <th>Name</th>
  56. <th>Location</th>
  57. </tr>
  58. <tr class="person">
  59. <td class="name">DummyName</td>
  60. <td class="location">DummyLocation</td>
  61. </tr>
  62. </table>
  63. </div>
  64. {pygmentize}
  65. There's the Scala code to create the transformer...
  66. {pygmentize:: scala}
  67. // add an implicit conversion from Transform -> NodeSeq
  68. import org.fusesource.scalate.scuery.Transform._
  69. val people = List(Person("James", "Mells"),
  70. Person("Hiram", "Tampa"))
  71. object transformer extends Transformer {
  72. $(".person") { node =>
  73. people.flatMap { p =>
  74. new Transform(node) {
  75. $(".name").contents = p.name
  76. $(".location").contents = p.location
  77. }
  78. }
  79. }
  80. }
  81. {pygmentize}
  82. Which would generate
  83. {pygmentize:: html}
  84. <div id="content">
  85. <table class="people">
  86. <tr>
  87. <th>Name</th>
  88. <th>Location</th>
  89. </tr>
  90. <tr class="person">
  91. <td class="name">James</td>
  92. <td class="location">Mells</td>
  93. </tr>
  94. <tr class="person">
  95. <td class="name">Hiram</td>
  96. <td class="location">Tampa</td>
  97. </tr>
  98. </table>
  99. </div>
  100. {pygmentize}