Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Aug 23, 2012
1 parent a98a80c commit 7a2b40d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions example/Records.hs
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions example/homePage.mustache
Original file line number Diff line number Diff line change
@@ -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>
3 changes: 3 additions & 0 deletions example/post.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 class="entry-title">{{entryTitle}}</h1>

<div class="entry-content">{{{entryContent}}}</div>
35 changes: 35 additions & 0 deletions example/showHomePage.hs
Original file line number Diff line number Diff line change
@@ -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.