2

In my Racket web-server/insta app, I have some repetitive <div> HTML that I want to factor out into a separate function and then call it with params to generate the div HTML inside a larger HTML xexpr. All that I get on the web page when I do this are the two parameter strings. How can I factor the <div> code out?

I tried simply calling the function from the quoted expression.
I also tried adding a response/xexpr in front of the quoted <div> code in the div function, but no go.

;; I want to factor the <div> out of this code into a separate function.

(define (start request)
  (response/xexpr
   '(html (head (title "Testing")
          (body (form
                  (div ((class "form-group"))
                       (label ((for "public-id")) "Public Id")
                       (input ((type "text")
                               (name  "public-id")
                               (id    "public-id")
                               (placeholder "Enter public id"))))

;; I tried doing that with this function, both with and without the "response/xexpr" code.

(define (input-field-html fld-name-id fld-caption)

 (response/xexpr

  '(div ((class "form-group"))
        (label ((for fld-name-id)) "Public Id")
        (input ((type "text")
                (name  fld-name-id)
                (placeholder fld-caption))))))
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
citrus
  • 53
  • 4
  • Can you show what you mean by "I tried simply calling the function from the quoted expression"? – Alex Knauth Apr 05 '19 at 15:16
  • This question might be relavant: [_Evaluating variables in Racket response/xexpr_](https://stackoverflow.com/questions/38646740/evaluating-variables-in-racket-response-xexpr) – Alex Knauth Apr 05 '19 at 15:20
  • Thanks for your responses Alex and for the link. The answer below works well. Thanks again. – citrus Apr 05 '19 at 19:15

1 Answers1

3
#lang web-server/insta


(define (start request)
  (response/xexpr
   `(html (head (title "Testing")
                (body (form
                       ,(input-field-html "public-id" "Enter public id")))))))


(define (input-field-html fld-name-id fld-caption)
  `(div ((class "form-group"))
        (label ((for ,fld-name-id)) "Public Id")
        (input ((type "text")
                (name  ,fld-name-id)
                (placeholder ,fld-caption)))))

See:

Atharva Shukla
  • 2,098
  • 8
  • 21
  • That worked. Thank you so much! I'm new to Racket and I see that I will have to spend some time learning macros. Thanks for the links as well! – citrus Apr 05 '19 at 19:03