[racket] Some more questions about using/composing Formlets

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Dec 29 19:57:20 EST 2011

On Thu, Dec 29, 2011 at 2:42 PM, J G Cho <gcho at fundingmatters.com> wrote:
> After tinkering with Formlets a bit, I have some questions for the experts.
>
> 1. I am trying to use Formlet with Template to piggy back
> on some given CSS/JSs (from Wufoo in this instance),
> which means I have to provide lots of #attributes and ((class "left
> right large small"))
> So, I am forced to do
>
>  (span
>  ,{(to-string
>     (required
>      (text-input #:attributes '((class "field text fn") (size "8") ))))
>    . => . first}
>  (label "First"))
>  (span
>  ,{(to-string
>     (required
>      (text-input #:attributes '((class "field text ln")  (size "14") ))))
>    . => . last}
>  (label "Last")))
>
> instead of using input-string.
>
> Is there a way to pass in #:attribute to input-string?

No, input-string was my original version that didn't have many hooks
and made big assumptions about how you want to use it. text-input is
more basic a building block.

>
> 2. Another consequence of using Formlets with given CSS/JSs is that
> code gets rather long and
> polluted with lots of ((class "blah blah blah")) and #:attributes (...)
> At the end of the day, it makes me wonder if it's worth the effort
> doing it this way
> (https://github.com/racketeer/racketeering/blob/master/wufooing/emergency-contact.rkt).
>
> Maybe I am better off with just using templates, any thoughts/advice
> on this?

You should define a formlet like

(define (classy-input-string [classes ""] [size "8"])
        (to-string
          (required
           (text-input #:attributes `((class (format "field text ~a"
        classes)) (size ,size) )))))

and use that instead. In general, a function/identifier exists to not duplicate
code, so whenever you would otherwise duplicate code... make a
function, formlets are no different.

>
> 3. Say I have a Formlet like this to be used in different places.
>
> (define name-formlet
>  (formlet
>     (div
>        (label "First Name")
>        ,{input-string . => . first}
>        (label "Last Name")
>        ,{input-string . => . last})
>     (list first last)))
>
>
> ...some where
>
> (formlet
>   (fieldset
>      (legend "Guy")
>      ,{name-formlet . =+ . guy}
>      ,{phone-formlet ...
>      ...
>
> ... else where
>
> (formlet
>   (fieldset
>      (legend "Gal")
>      ,{name-formlet . =+ . gal}
>      ,{phone-formlet ...
>      ...
>
> I want the label and input tags to come out without div tag and become
> children of fieldset tag so I end up with
>
> <fieldset>
> <legend>
> <label><input ....
> <label><input ....
>
> instead of
>
> <fieldset>
> <legend>
> <div>
> <label><input ....
> <label><input ....
>
> Is there a way to do this? It seems I cannot define like this:
> (define name-formlet
>  (formlet
>     (list-that-is-not-tag???
>        (label "First Name")
>        ,{input-string . => . first}
>        (label "Last Name")
>        ,{input-string . => . last})
>     (list first last)))
>
> or like
> (define name-formlet
>  (formlet
>        ;things, with last one being the bound names...
>        (label "First Name")
>        ,{input-string . => . first}
>        (label "Last Name")
>        ,{input-string . => . last}
>       (list first last) ))

This is exactly why formlets are rendered as xexpr forests and why the
(#%# ...) form exists. You want...

(define name-formlet
 (formlet
  (#%#
   (label "First Name")
   ,{input-string . => . first}
   (label "Last Name")
   ,{input-string . => . last})
  (list first last)))

Jay

>
>
> Regards,
>
> jGc
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93



Posted on the users mailing list.