【问题标题】:Snap Framework: Compiled splices and processing forms with digestive functorsSnap 框架:使用消化函子编译拼接和处理表格
【发布时间】:2013-04-21 03:21:55
【问题描述】:

我正在尝试了解已编译的拼接以及如何将它们与消化函子形式一起使用。谁有代码示例?

【问题讨论】:

  • 你那里的东西对我来说看起来不错。您有更具体的问题吗?
  • 感谢mightybyte。我找不到任何与编译拼接一起使用的 dig-func 形式的示例,只是想要一些关于如何将它们一起使用并利用效率收益的指导。
  • 这是一个很好的例子。也许你可以改变你的问题,所以它只是要求一个例子?然后用你在这里的代码自己回答,这样它就会显示为一个已回答的问题。

标签: haskell haskell-snap-framework digestive-functors


【解决方案1】:

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

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多