Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hamlet in Haskell without Yesod

Tags:

haskell

hamlet

Can anyone point me to an example of how to use Hamlet without Yesod? http://www.yesodweb.com/book/templates is a great bit of documentation, but I can't get my ghci session to render even a simple hamlet template without crashing.

like image 567
singpolyma Avatar asked Jul 15 '11 20:07

singpolyma


2 Answers

Here's an example showing most of the basic stuff, including rendering of typed URLs.

{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}

import Data.Text
import Text.Blaze.Html.Renderer.String (renderHtml)
import Text.Hamlet hiding (renderHtml)

data Url = Haskell | Yesod

renderUrl Haskell _ = pack "http://haskell.org"
renderUrl Yesod   _ = pack "http://www.yesodweb.com"

title = pack "This is in scope of the template below"

template :: HtmlUrl Url
template = [hamlet|
<html>
    <head>
        #{title}
    <body>
        <p>
            <a href=@{Haskell}>Haskell
            <a href=@{Yesod}>Yesod
|]

main = do
    let html = template renderUrl
    putStrLn $ renderHtml html

Output:

<html><head>This is in scope of the template below</head>
<body><p><a href="http://haskell.org">Haskell</a>
<a href="http://www.yesodweb.com">Yesod</a>
</p>
</body>
</html>
like image 91
hammar Avatar answered Oct 28 '22 18:10

hammar


Well, handwaving the URL rendering and doing things in the stupidest way that works, we can use this:

hamVal = [$hamlet| 
<html>
    <head>
        <title>Test page
    <body>Testing
|]

test :: ByteString
test = renderHamlet (\_ _ -> "") hamVal

Which works as expected. I imagine you want to do something slightly more useful, but the trivial example here works fine so it's hard to say more without knowing where you're having trouble.

like image 22
C. A. McCann Avatar answered Oct 28 '22 19:10

C. A. McCann