Skip to content
Browse files

example

  • Loading branch information...
1 parent a98a80c commit 7a2b40dfdc9e06f20ca9c20c85683a66ee8e1c00 @singpolyma committed Aug 22, 2012
Showing with 83 additions and 0 deletions.
  1. +5 −0 example/Makefile
  2. +20 −0 example/Records.hs
  3. +20 −0 example/homePage.mustache
  4. +3 −0 example/post.mustache
  5. +35 −0 example/showHomePage.hs
View
5 example/Makefile
@@ -0,0 +1,5 @@
+showHomePage: showHomePage.hs MustacheTemplates.hs Records.hs
+ ghc -Wall -fno-warn-name-shadowing $^ -o $@
+
+MustacheTemplates.hs: homePage.mustache post.mustache
+ ../mustache2hs -m Records.hs homePage.mustache Blog post.mustache Post > MustacheTemplates.hs
View
20 example/Records.hs
@@ -0,0 +1,20 @@
+module Records where
+
+import Text.Blaze.Html
+import Text.Blaze.Html.Renderer.String
+import Text.PrettyPrint.Leijen
+
+newtype HTML = HTML Html
+
+data Blog = Blog {
+ postCount :: Int,
+ posts :: [Post]
+}
+
+data Post = Post {
+ entryTitle :: String,
+ entryContent :: HTML
+}
+
+instance Pretty HTML where
+ pretty (HTML html) = string $ renderHtml html
View
20 example/homePage.mustache
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Blog Home</title>
+ </head>
+
+ <body>
+ {{#postCount}}
+ <ol class="hfeed">
+ {{#posts}}
+ <li class="hentry">
+ <section>
+ {{> post.mustache}}
+ </section>
+ </li>
+ {{/posts}}
+ </ol>
+ {{/postCount}}
+ </body>
+</html>
View
3 example/post.mustache
@@ -0,0 +1,3 @@
+<h1 class="entry-title">{{entryTitle}}</h1>
+
+<div class="entry-content">{{{entryContent}}}</div>
View
35 example/showHomePage.hs
@@ -0,0 +1,35 @@
+module Main where
+
+import Text.Blaze.Internal
+import Text.Blaze.Html5
+import Blaze.ByteString.Builder
+import qualified Data.ByteString as BS
+import qualified Data.Text as T
+
+import Records
+import MustacheTemplates
+
+htmlEscape :: String -> String
+htmlEscape = concatMap escChar
+ where
+ escChar '&' = "&amp;"
+ escChar '"' = "&quot;"
+ escChar '<' = "&lt;"
+ escChar '>' = "&gt;"
+ escChar c = [c]
+
+main :: IO ()
+main = toByteStringIO BS.putStr $
+ homePage htmlEscape (Blog {
+ postCount = 2,
+ posts = [
+ Post {
+ entryTitle = "This is a post!",
+ entryContent = HTML $ text $ T.pack "And the contents of that post!"
+ },
+ Post {
+ entryTitle = "Earlier post...",
+ entryContent = HTML $ b $ text $ T.pack "Text all bold"
+ }
+ ]
+ })

0 comments on commit 7a2b40d

Please sign in to comment.
Something went wrong with that request. Please try again.