Skip to content

Instantly share code, notes, and snippets.

@julienrf
Created April 11, 2011 21:06
Show Gist options
  • Save julienrf/914346 to your computer and use it in GitHub Desktop.
Save julienrf/914346 to your computer and use it in GitHub Desktop.
Template sample using Glitter
// Import the Glitter DSL
import glitter._
object Templates {
// Define a reusable layout
def layout(body: Xml) =
html5dtd | 'html (
'head :: 'title :: "Glitter is amazing!"
| 'body :: body
)
// Define a template taking one String argument and using the layout defined above
def show(name: String) =
layout (
'h1 :: "Show user"
| 'p :: ("Hello " | 'strong(name) | "!")
)
// Define a template taking a List of Strings, using the layout defined above
def index(users: List[String]) =
layout (
'h1 :: "User list"
| 'ul % 'class~"user-list" :: (for (user <- users) yield ('li :: user))
)
}
scala> Templates.show("Julien").render
res6: String =
<!DOCTYPE html>
<html>
<head>
<title>Glitter is amazing!</title>
</head>
<body>
<h1>Show user</h1>
<p>Hello <strong>Julien</strong>!</p>
</body>
</html>
scala> Templates.index(List("Julien", "Paul", "Christopher")).render
res7: String =
<!DOCTYPE html>
<html>
<head>
<title>Glitter is amazing!</title>
</head>
<body>
<h1>User list</h1>
<ul class="user-list">
<li>Julien</li>
<li>Paul</li>
<li>Christopher</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment