簡體   English   中英

Snap框架:使用消化函數編譯拼接和處理表單

[英]Snap Framework: Compiled splices and processing forms with digestive functors

我正在嘗試理解編譯的拼接以及如何將它們與消化函子形式一起使用。 有人有任何代碼示例嗎?

以下工作用於處理已編譯拼接中的表單...

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

“newBook”模板

New Book Entry:
<br>

<bookForm action="/newBook">

<dfChildErrorList ref="" />

<dfInputText ref="title"/>
<dfInputText ref="description"/>
<dfInputSubmit value="submit"/>
</bookForm>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM