【问题标题】:with-html-output adds string when using :liwith-html-output 在使用 :li 时添加字符串
【发布时间】:2014-04-29 23:21:31
【问题描述】:

我正在通过 Adam Tornhill 的 Lisp For The Web 进行工作,但我一直在生成一个包含 li 元素的 html 页面。

(with-html-output (*standard-output* nil :prologue t :indent t)
   (htm
       (:li (:a :href "Link" "Vote!")
        )))

当我编译它时,以下输出会打印到 REPL

(with-html-output (*standard-output* nil :prologue t :indent t)
   (htm 
       (:li (:a :href "Link" "Vote!")
        )))
 <!DOCTYPE html>

<li>
  <a href='Link'>Vote!
  </a>
</li>
"
<li>
  <a href='Link'>Vote!
  </a>     
</li>"   

通常不添加输出末尾的字符串,并且包含该字符串的站点不会在 hunchentoot 中呈现。在 :li 周围添加 :ol 并没有帮助,我想保持示例最小化。

书中的代码作为参考:

(define-easy-handler (retro-games :uri "/retro-games") () 
  (standard-page (:title "Top Retro Games") 
   (:h1 "Vote on your all time favourite retro games!") 
   (:p "Missing a game? Make it available for votes " (:a :href "new-game" "here")) 
   (:h2 "Current stand") 
   (:div :id "chart" ; Used for CSS styling of the links. 
     (:ol 
 (dolist (game (games)) 
  (htm   
   (:li (:a :href (format nil "vote?name=~a" (escape-string ; avoid injection attacks 
                                                 (name game))) "Vote!") 
         (fmt "~A with ~d votes" (name game) (votes game)))))))))

【问题讨论】:

    标签: common-lisp sbcl cl-who


    【解决方案1】:

    您首先看到的是表单在评估时打印*standard-output* 的内容。之后看到的字符串是表单的 result,由 REPL 打印。由于您的 Hunchentoot 处理程序只对输出流中的内容感兴趣,因此结果并不重要。

    要简单地以字符串形式获取结果,您可以使用with-html-output-to-string

    (with-html-output-to-string (str nil :prologue t :indent t)
      (htm 
       (:li (:a :href "Link" "Vote!"))))
    

    另一方面,要抑制结果字符串并只看到写出的文档,您可以执行以下操作:

    (progn
      (with-html-output (*standard-output* nil :prologue t :indent t)
        (htm 
         (:li (:a :href "Link" "Vote!"))))
      (values))
    

    【讨论】:

    • 今天,当我再次尝试时,由于某种原因它起作用了。感谢您对表单结果与输出流的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多