2017-04-18 56 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做了这个职位解决你的问题?如果是这样,请不要忘记接受它;)谢谢! –