packages feed

happstack-0.2.1: templates/project/src/AppView.hs

{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses #-}
{-# OPTIONS_GHC -F -pgmFtrhsx #-}
module AppView where

import AppState
import HSP
import System.Locale (defaultTimeLocale)
import System.Time (formatCalendarTime, toUTCTime)
import Control.Monad.Trans (MonadIO)
import Happstack.Server.HStringTemplate (webST)
import Happstack.Server.HSP.HTML (webHSP)

-- Convenience Functions
dateStr ct =
  formatCalendarTime
    defaultTimeLocale
    "%a, %B %d, %Y at %H:%M:%S (UTC)"
    (toUTCTime ct)

-- Main Implementation
instance (XMLGenerator m) => (EmbedAsChild m (GuestBookEntry, Bool)) where
    asChild ((GuestBookEntry author message date), alt) =
        <%
           <li class=(if alt then "alt" else "")>
            <strong><% author %></strong> said:<br /><br />
            <% map p (lines message) %>
            <br />
            <small class="commentmetadata"><% dateStr date %></small> 
           </li>
         %>
        where p str = <p><% str %></p>

instance (XMLGenerator m) => (EmbedAsChild m GuestBook) where
    asChild (GuestBook entries) = 
        <% 
         <div>
          <h2 id="comments" class="h2comment">Words of Wisdom</h2>
          <div class="clear" />
          <ul class="commentlist">
           <% zip entries (cycle [False,True]) %>
          </ul>
         </div>
        %>

renderFromBody title body = webHSP $ pageFromBody title body
pageFromBody :: (EmbedAsChild (HSPT' IO) xml) => String -> xml -> HSP XML
pageFromBody title body =
    withMetaData html4Strict $
    <html>
     <head>
      <title><% title %></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <link rel="stylesheet" type="text/css" href="/theme/style.css" media="screen" />
     </head>
     <body>
      <div id="header">
       <div class="grunge"></div>
       <div class="peel"></div>
       <div class="topnavi">
        <ul>
         <li class="current_page_item"><a href="/" title="Guestbook">Guestbook</a></li>
        </ul>
       </div>
      </div>

      <div class="side1">
       <div class="sbar_section">
        <h2>Links</h2>
         <ul>
      	  <li class="cat-item cat-item-1"><a href="http://happstack.com/" title="happstack" accesskey="H"><span class="accesskey">H</span>appstack</a></li>
      	  <li class="cat-item cat-item-2"><a href="http://happstack.com/tutorials.html" title="happstack" accesskey="T"><span class="accesskey">T</span>utorials</a></li>
         </ul>
       </div>
      </div>

      <div class="wrap">
       <div class="innercont_main">

        <div class="post">
         <div class="posttop">
          <div class="date">14<div>Feb</div></div>
          <h1 class="posttitle">Happstack Guestbook</h1>
          <div class="storycontent">
           <p>
              Hey congrats! You're using
              <a href="http://happstack.com">Happstack</a> 0.2.
              This is a guestbook example which you can freely change to your
              whims and fancies.
            </p>
            <p>
              This page is written using Haskell Server Pages (HSP). For an example
              of a page using HStringTemplate, look at the
              <a href="/README">dynamic README</a>.
            </p>
           <p>Leave a message for the next visitor here...</p>
           <form action="/entries" method="post" enctype="multipart/form-data:charset=UTF-8" accept-charset="UTF-8">
            <p><label for="author">A<span class="accesskey">u</span>thor</label><br /><input type="text" name="author" id="author" tabindex="1" accesskey="U" /></p>
            <p><label for="message"><span class="accesskey">M</span>essage</label><br /><textarea cols="80" rows="10" name="message" id="message" tabindex="2" accesskey="M"></textarea></p>
            <p><input type="submit" tabindex="3" accesskey="L" value="Leave GuestBook Entry" /></p>
           </form>
          </div>
         </div>
        </div>

        <% body %>
       </div>
      </div>
           
      <div class="footer">
        <div class="finalfooter">Theme : <a href="http://www.dezinerfolio.com/2007/10/10/just-another-wodpress-theme" title="sIMPRESS v2 theme">sIMPRESS v2</a> by <a href="http://dezinerfolio.com" title="Dezinerfolio">Dezinerfolio</a></div>
      </div>

     </body>
    </html>

renderREADME now = do
  webST "readme" [("time", dateStr now)]