Created
April 11, 2011 21:06
-
-
Save julienrf/914346 to your computer and use it in GitHub Desktop.
Template sample using Glitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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