yesodweb / yesod

A RESTful Haskell web framework built on WAI.
http://www.yesodweb.com/
MIT License
2.63k stars 374 forks source link

Function renderBootstrap #212

Closed meteficha closed 12 years ago

meteficha commented 12 years ago

Hello!

Bootstrap is that CSS template by Twitter. renderBootstrap is like renderDivs, but uses the structure that Bootstrap expects. I don't know if this is useful enough to be included on yesod-forms, hence the issue and not a pull request =).

-- | Render a form using Bootstrap-friendly HTML syntax.  Based
-- on 'renderDivs'.
renderBootstrap :: FormRender sub master a
renderBootstrap aform fragment = do
    (res, views') <- aFormToForm aform
    let views = views' []
        has (Just _) = True
        has Nothing  = False
    let widget = [whamlet|
\#{fragment}
$forall view <- views
    <div .clearfix :fvRequired view:.required :not $ fvRequired view:.optional :has $ fvErrors view:.error>
        <label for=#{fvId view}>#{fvLabel view}
        <div.input>
            ^{fvInput view}
            $maybe tt <- fvTooltip view
                <span .help-block>#{tt}
            $maybe err <- fvErrors view
                <span .help-block>#{err}
|]
    return (res, widget)

What do you think?

pbrisbin commented 12 years ago

I like it, a lot. I just started using bootstrap on one of my projects and this would be perfect. We already have code in the framework for projects like normalize, html5-boilerplate, yepnope, etc so I dont' see why we couldn't provide something like this.

I'll let the others comment.

gregwebs commented 12 years ago

I think this is a great idea. It would be great to provide users out of the box with some good styling (but let them blow it away and use their own custom styling).

snoyberg commented 12 years ago

I'm in favor as well.

snoyberg commented 12 years ago

Do you want me to copy this code into yesod-form?

meteficha commented 12 years ago

Yes, please do it. Here's an example usage to be included in the docs:

        <form method=post action=@{ActionR} enctype=#{formEnctype}>
          <fieldset>
            <legend>_{MsgLegend}
            $case result
              $of FormFailure reasons
                $forall reason <- reasons
                  <div .alert-message .error>#{reason}
              $of _
            ^{formWidget}
            <div .actions>
              <input .btn .primary type=submit value=_{MsgSubmit}>

This assumes something like

((result, formWidget), formEnctype) <- generateFormPost myform