Arjun
Arjun

Reputation: 1711

Racket: inserting string variables into xexpression attributes

I'm trying to display an IMG tag using response/xexpr

This works:

(define (start req)
  (response/xexpr
   '(html (head (title "Racket Heroku App"))
          (body 
            (h1 "Hello World!")
            (div "it works!!!!")
            (img ([src "https://docs.racket-lang.org/pict/pict_196.png"]))))))

This does not:

(define example-url "https://docs.racket-lang.org/pict/pict_196.png")

(define (start req)
  (response/xexpr
   '(html (head (title "Racket Heroku App"))
          (body 
            (h1 "Hello World!")
            (div "it works!!!!")
            (img ([src example-url]))))))

The error shown is the following:

response/xexpr: contract violation;
 Not an Xexpr. Expected an attribute value string, given 'a

Context:

'(html
  (head (title "Racket Heroku App"))
  (body
   (h1 "Hello World!")
   (div "it works!!!!")
   (img
    ((src
      'a)))))

What am I doing wrong?

Upvotes: 0

Views: 150

Answers (1)

Óscar López
Óscar López

Reputation: 236124

You're not evaluating the value of the example-url variable, you're literally passing example-url as the URL. Try this instead:

(define example-url "https://docs.racket-lang.org/pict/pict_196.png")

(define (start req)
  (response/xexpr
   `(html (head (title "Racket Heroku App"))
          (body 
            (h1 "Hello World!")
            (div "it works!!!!")
            (img ([src ,example-url]))))))

In the above, I'm using quasiquoting for evaluating the variable.

Upvotes: 1

Related Questions