2013-04-26 17 views
5

我想了解編譯拼接,以及如何使用它們與消化仿函數形式。任何人有任何代碼示例?捕捉框架:編譯拼接和處理形式與消化仿函數

+0

你在那裏看起來不錯。你有更具體的問題嗎? – mightybyte 2013-04-29 17:23:28

+0

謝謝mightybyte。我還沒有找到任何與編譯接頭一起使用的dig-func表單的例子,只是想了解如何一起使用它們並利用效率提高的一些指導。 – 2013-04-30 23:45:41

+0

這是一個很好的例子。也許你可以改變你的問題,所以它只是要求一個例子?然後用你在這裏的代碼自己回答,所以它會顯示爲一個回答的問題。 – mightybyte 2013-05-01 23:05:50

回答

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 

的 「newBook」 模板

New Book Entry: 
<br> 

<bookForm action="/newBook"> 

<dfChildErrorList ref="" /> 

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