[Add example of global queries. Lemmih **20080420084020] { adddir ./examples/global adddir ./examples/global/pages addfile ./examples/global/Global.hs hunk ./examples/global/Global.hs 1 +{-# OPTIONS -fglasgow-exts -fth -fallow-undecidable-instances #-} +module Global where + +import HAppS.Template.HSP +import HAppS.Template.HSP.Handle + +import HAppS.Server + +import Text.RJson +import Data.Generics.SYB.WithClass.Derive + +data News = News { headline :: String + , fulltext :: String } + +$(derive [''News]) + +flat x = ok (toResponse x) + +main :: IO () +main = do store <- newStore -- The store must be created outside of 'simpleHTTP'. + simpleHTTP nullConf + [ -- The templates are in pages/ and object files should be placed in objfiles/ + runHSPHandle "pages" "objfiles" store $ + multi [ dir "json" $ [ dir "news" + [ anyRequest $ -- This can be access both from the server side and client side. + flat $ toJson [ News "HAppS+HSP" "HSP can now be used as the \ + \templating engine for HAppS" + , News "Slow news day" "Nothing much happened" ] + ]] + , method GET $ do -- Template parameters are stored as json data + addParam "title" "This is app data" + flat =<< execTemplate "Index.hs" + ] + ] addfile ./examples/global/pages/Index.hs hunk ./examples/global/pages/Index.hs 1 +module Index where + +import HSP +import HAppS.Template.HSP + +import Text.RJson +import qualified Data.Map as Map + + +page :: Web XML +page = + <% localRead "title" :: Web String %> + +

News:

+ <% do JDArray news <- globalQuery $ simpleRequest "/json/news/" +
<% map format news %>
+ %> + + + +format json = [
<% json#"headline" %>
+ ,
<% json#"fulltext" %>
] + +JDObject obj # key = Map.findWithDefault (JDString $ "Key not found: " ++ key) key obj +json # key = json + +instance Monad m => IsXMLs m JsonData where + toXMLs (JDString str) = toXMLs str + toXMLs json = toXMLs (show json) + }