jaspervdj / digestive-functors

A general way to consume input using applicative functors
149 stars 71 forks source link

Injecting custom splices [digestive-functors-heist] #134

Open cimmanon opened 8 years ago

cimmanon commented 8 years ago

I have a number of forms that have immutable data that I would like to present to the user, but the only way I can do so with digestive-functors-heist (without binding additional splices into the template myself) is with some sort of text input element (dfInputText, dfInputTextArea, etc.). This ends up making a rather unattractive form and requires disabling the input element to reinforce the fact that it is immutable.

My first attempt at working around this was to create a copy of digestiveSplices that calls the original function in addition to my custom splice. This worked fine for the top level form, but it doesn't propagate down into sub views (dfSubView) or lists (dfInputList) because those splices still call the original digestiveSplices and bindDigestiveSplices functions. I ended up hacking the digestiveSplices function to add my custom splice for rendering the plain text contents. This seemed fine at first, but now I find myself wanting more custom splices... and going back and hacking the base library each time I need something new is less than desirable.

I would like to propose creating alternate versions of the digestiveSplices and bindDigestiveSplices functions that accepts a (View Text -> Splices (Splice m)) as its first argument.

digestiveSplices' :: MonadIO m => (View Text -> Splices (Splice m)) -> View Text -> Splices (Splice m)
digestiveSplices' splices view = do
    "dfInput"            ## dfInput view
    "dfInputList"        ## dfInputList' splices view
    "dfInputText"        ## dfInputText view
    "dfInputTextArea"    ## dfInputTextArea view
    "dfInputPassword"    ## dfInputPassword view
    "dfInputHidden"      ## dfInputHidden view
    "dfInputSelect"      ## dfInputSelect view
    "dfInputSelectGroup" ## dfInputSelectGroup view
    "dfInputRadio"       ## dfInputRadio view
    "dfInputCheckbox"    ## dfInputCheckbox view
    "dfInputFile"        ## dfInputFile view
    "dfInputSubmit"      ## dfInputSubmit view
    "dfLabel"            ## dfLabel view
    "dfForm"             ## dfForm view
    "dfErrorList"        ## dfErrorList view
    "dfChildErrorList"   ## dfChildErrorList view
    "dfSubView"          ## dfSubView' splices view
    "dfIfChildErrors"    ## dfIfChildErrors view
    splices view

I've done a quick modification of the Interpreted splices: https://gist.github.com/cimmanon/f40e706dcf2d3370c09380edded9ddce