5

我试图了解编译的拼接以及如何将它们与消化函子形式一起使用。有人有任何代码示例吗?

4

1 回答 1

3

以下工作用于处理已编译拼接中的表单...

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

附加应用程序、数据、渲染代码...

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

“新书”模板

New Book Entry:
<br>

<bookForm action="/newBook">

<dfChildErrorList ref="" />

<dfInputText ref="title"/>
<dfInputText ref="description"/>
<dfInputSubmit value="submit"/>
</bookForm>
于 2013-05-02T23:30:04.493 回答