Search code examples
haskellhaskell-snap-frameworkhamlet

Snap render list in Hamlet


Given this little project I'm using to learn Haskell, I would like to move my request handler's code generation to a Hamlet template, but am unsure how to pass things around.

My current code generates the following error when lines are uncommented, which is the first blocker:

Couldn't match expected type `String -> String'
                with actual type `String'
    In the return type of a call of `renderHtml'
    Probable cause: `renderHtml' is applied to too many arguments
    In the expression: renderHtml ($ (shamletFile "fileList.hamlet"))
    In an equation for `myTemplate':
        myTemplate = renderHtml ($ (shamletFile "fileList.hamlet"))

Code:

site :: Snap ()
site =
    ifTop (writeBS "hello world") <|> 
    route [ ("foo", writeBS "ba"),
            ("view_root_json_files", listRootFilesHandler)
          ] <|> 
    dir "static" (serveDirectory ".")

--myTemplate :: String -> String
--myTemplate = renderHtml ( $(shamletFile "fileList.hamlet") )

toText :: [FilePath] -> Text
toText = foldMap (flip snoc '\n' . pack)

listRootFilesHandler :: Snap ()
listRootFilesHandler = do
    filenames <- liftIO $ getDirectoryContents "data"
    let filtered_filenames = filter (not . isPrefixOf ".") filenames
    writeText $ toText filtered_filenames

Solution

  • Ghc is telling you the correct type signature to put there. Just replace String -> String with String.