Search code examples
haskelldigestive-functors

Form elements with digestive-functors


Digestive-functors-blaze creates html like <input type="text" id="foo" name="foo" /><label for="foo">Bar</label>, but I didn't find any standard way to for example add <br /> to the end.

I came up with this:

br :: (Monad m) => HappstackForm m H.Html BlazeFormHtml ()
br = Common.label $ \_ -> do
  createFormHtml $ \cfg -> do
    H.br

I can append it with <++ which does what I expect it to, but I wonder whether this is the intended way?

Also how would this extend with for example fieldsets.


Solution

  • This is the intended way, although br would be a little cleaner when written as:

    br :: Monad m
       => Form m i e BlazeFormHtml ()
    br = view $ createFormHtml $ const H.br
    

    Anyway, I figured this definition is a little cumbersome so I added a viewHtml function to Text.Digestive.Forms.Html:

    viewHtml :: Monad m => a -> Form m i e (FormHtml a) ()
    viewHtml = view . createFormHtml . const
    

    This is available in digestive-functors-0.1.0.1. Using this new combinator, you should be able to just use <++ viewHtml H.br — I hope this helps.