2017-04-18 55 views
0

我試圖使用顯示的IMG標籤響應/ xexpr球拍:將字符串變量變成xexpression屬性

這工作:

(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"])))))) 

這不:

(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])))))) 

的顯示的錯誤如下:

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))))) 

我在做什麼錯?

回答

1

您並未評估example-url變量的值,而是將example-url作爲URL傳遞。試試這個:

(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])))))) 

在上面,我使用quasiquoting評估變量。

+0

@Arjun做了這個職位解決你的問題?如果是這樣,請不要忘記接受它;)謝謝! –