简体   繁体   中英

Snap Framework: Compiled splices and processing forms with digestive functors

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM