5

I'm trying to understand compiled splices and how to use them with digestive functor forms. Anyone have any code examples?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
T.Roth
  • 145
  • 6
  • What you have there looks good to me. Do you have a more specific question? – mightybyte Apr 29 '13 at 17:23
  • Thanks mightybyte. I haven't been able to find any examples of dig-func forms used with compiled splices and just wanted some guidance on how to use them together and take advantage of the efficiency gains. – T.Roth Apr 30 '13 at 23:45
  • This is a nice example. Maybe you could change your question so it just asks for an example? Then answer it yourself with the code you have here so it will show up as an answered question. – mightybyte May 01 '13 at 23:05

1 Answers1

3

The following works for processing the form in the compiled splice...

bookFormSplice :: C.Splice (Handler App App)
bookFormSplice = formSplice $ do
  (view,result) <- DFS.runForm "bookForm" bookForm -- runForm is in Text.Digestive.Snap
  case result of Just x -> redirect "/" --valid result, redirect to the home page
                                        --can also insert into DB here
                 Nothing -> return view --no result or invalid form data,
                                        --return the view and render the form page

Additional application,data,render code...

data Book = Book { title :: T.Text
               , description :: T.Text }


bookForm :: Monad m => Form T.Text m Book
bookForm = check "Cannot be blank" notBlank $ Book
    <$> "title" .: text (Nothing)
    <*> "description" .: text Nothing
    where
      notBlank (Book t d) = t /= "" && d /= ""




handleNewBook :: Handler App App ()
handleNewBook = cRender "newBook"



routes :: [(ByteString, Handler App App ())]
routes = [ ("/login",    with auth handleLoginSubmit)
     , ("/logout",   with auth handleLogout)
     , ("/new_user", with auth handleNewUser)
     , ("/newBook", handleNewBook)
     , ("",          serveDirectory "static")
     ]


app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
h <- nestSnaplet "" heist $ heistInit "templates"
s <- nestSnaplet "sess" sess $
       initCookieSessionManager "site_key.txt" "sess" (Just 3600)
a <- nestSnaplet "auth" auth $
       initJsonFileAuthManager defAuthSettings sess "users.json"

let config = mempty { hcCompiledSplices = [("bookForm", bookFormSplice)]}
addConfig h config
addRoutes routes
addAuthSplices auth
return $ App h s a

The "newBook" template

New Book Entry:
<br>

<bookForm action="/newBook">

<dfChildErrorList ref="" />

<dfInputText ref="title"/>
<dfInputText ref="description"/>
<dfInputSubmit value="submit"/>
</bookForm>
T.Roth
  • 145
  • 6