Search code examples
formsroutesyesodhamlet

How to use hamlet template with route parameter?


I'm working on a yesod app based on the yesod-postgres stack template. I have a route defined in config/routes that has the form:

foo/edit/#Text EditFooR GET

In my hamlet template, I want to write

<form method=post action=@{EditFooR}#forms enctype=#{formEnctype}>
                    ^{formWidget}

                    <button type="submit">
                        Submit

and in my Handler I'd like to write:

getEditFooR :: T.Text -> Handler Html
getEditFooR name = do
    ....
    text <- findTextByName name

    (formWidget, formEnctype) <- generateFormPost (editFooForm text)
    defaultLayout $ do
        $(widgetFile "editFoo")

Except that I need to provide the parameter to the @{EditFooR} route. How is this done in the Hamlet file/Handler?


Solution

  • The answer is that the .hamlet template should have the format:

    <form method=post action=@{EditFooR fooName}#forms enctype=#{formEnctype}>
                    ^{formWidget}
    
                    <button type="submit">
                        Submit
    

    and the Handler should have the format:

    getEditFooR :: T.Text -> Handler Html
    getEditFooR name = do
        ....
        text <- findTextByName name
        fooName <- "something or other"
    
       (formWidget, formEnctype) <- generateFormPost (editFooForm text)
           defaultLayout $ do
           $(widgetFile "editFoo")