hub.darcs.net :: stepcut -> happstack -> annotate -> happstack-hstringtemplate/src/Happstack/Server/HStringTemplate.hs

Official Happstack Repository (http://www.happstack.com/)

happstack-server: more haddock improvements jeremy@n-heptane.com Tue Feb 1 22:06:57 UTC 2011
1
-- | This module provides support for using HStringTemplate with Happstack. <http://hackage.haskell.org/package/HStringTemplate>
now that we dropped GHC 6.8, s/PatternSignatures/ScopedTypeVariables/ jeremy@n-heptane.com Mon May 10 21:19:25 UTC 2010
2
{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, ScopedTypeVariables #-}
Oops, added missing Happstack.Server.HStringTemplate Matthew Elder <matt@mattelder.org> Sun Feb 22 21:59:10 UTC 2009
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Happstack.Server.HStringTemplate (webST) where

import Happstack.Server
  ( ToMessage(toMessage, toContentType, toResponse)
  , Response
  )
import Control.Monad.Trans (MonadIO, liftIO)
import Text.StringTemplate
  ( STGroup
  , StringTemplate
  , directoryGroupLazy
  , getStringTemplate
  , render
  , setManyAttrib
  )
import qualified Data.ByteString.Char8 as B (pack)
import qualified Data.ByteString.Lazy.Char8 as L (pack)
import System.Log.Logger (Priority(..), logM)
log' :: Priority -> String -> IO ()
log' = logM "Happstack.Server.HStringTemplate"

-- FIXME: Caveat, assumes text/html, can this be handled at the filter level?
instance ToMessage (StringTemplate String) where
Making the content type for String Templates text/html mattmunz@mac.com Sat May 8 04:53:53 UTC 2010
27
    toContentType _ = B.pack "text/html;charset=utf-8"
Oops, added missing Happstack.Server.HStringTemplate Matthew Elder <matt@mattelder.org> Sun Feb 22 21:59:10 UTC 2009
28
29
    toMessage = L.pack . render
inferIxSet and other doc strings wchogg@gmail.com Tue Apr 7 18:40:51 UTC 2009
30
-- | @webST name attrs@ renders a name template with attrs
happstack-hstringtemplate: split HStringTemplate support into happstack-hstringtemplate package jeremy@n-heptane.com Thu Jan 27 05:07:39 UTC 2011
31
32
33
34
webST :: (MonadIO m) => 
         String  -- ^ template name
      -> [(String, String)] -- ^ key/value attributes
      -> m Response
Oops, added missing Happstack.Server.HStringTemplate Matthew Elder <matt@mattelder.org> Sun Feb 22 21:59:10 UTC 2009
35
36
37
38
39
40
41
42
43
webST name attrs = do
  grp :: STGroup String <- liftIO $ directoryGroupLazy "templates"
  case getStringTemplate name grp of
    Just tmp -> do
      -- FIXME: I would rather use show, but StringTemplate String has no Show instance
      liftIO $ log' INFO ("webST executing: " ++ name)
      return . toResponse $ setManyAttrib attrs tmp
    Nothing  -> fail $ "Template does not exist: " ++ name