packages feed

happstack-hstringtemplate (empty) → 6.0.0

raw patch · 4 files changed

+105/−0 lines, 4 filesdep +HStringTemplatedep +basedep +bytestringsetup-changed

Dependencies added: HStringTemplate, base, bytestring, happstack-server, hslogger, mtl

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2010, Jeremy Shaw++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Jeremy Shaw nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ happstack-hstringtemplate.cabal view
@@ -0,0 +1,30 @@+Name:                happstack-hstringtemplate+Version:             6.0.0+Synopsis:            Support for using HStringTemplate in Happstack+Description:         Happstack is a web application framework. HStringTemplate is a templating solution.+Homepage:            http://www.happstack.com/+License:             BSD3+License-file:        LICENSE+Author:              Jeremy Shaw+Maintainer:          Happstack team <happs@googlegroups.com>+-- Copyright:           +Category:            Web+Build-type:          Simple+Cabal-version:       >=1.6++source-repository head+    type:     darcs+    subdir:   happstack-hstringtemplate+    location: http://patch-tag.com/r/mae/happstack++Library+  Hs-source-dirs:      src+  Exposed-modules:     Happstack.Server.HStringTemplate+  Build-depends:       base >= 3.0 && < 5.0,+                       bytestring,+                       happstack-server >= 6.0 && < 6.1,+                       hslogger,+                       HStringTemplate >= 0.4.3 && < 0.7,+                       mtl >= 1.1 && < 2.1++
+ src/Happstack/Server/HStringTemplate.hs view
@@ -0,0 +1,43 @@+-- | This module provides support for using HStringTemplate with Happstack. <http://hackage.haskell.org/package/HStringTemplate>+{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, ScopedTypeVariables #-}+{-# 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+    toContentType _ = B.pack "text/html;charset=utf-8"+    toMessage = L.pack . render++-- | @webST name attrs@ renders a name template with attrs+webST :: (MonadIO m) => +         String  -- ^ template name+      -> [(String, String)] -- ^ key/value attributes+      -> m Response+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+