cl-who 中的格式无法正常工作

发布于 2024-10-08 13:55:40 字数 1957 浏览 0 评论 0原文

我正在尝试通过 hunchentoot 和 cl-who 构建一个个人网站,但在以下代码中出现语义错误:

(defun index ()
  (standart-page (:title "~apb")
                 (dolist (article (articles))
                   (cl-who:htm
                    (:ul
                     (:li (format nil "~a: ~a" (article-date article) (article-title article))))))))

“standart-page”是一个宏:

(defmacro standart-page ((&key title) &body body)   `(cl-who:with-html-output-to-string (*standart-output* nil :prologue t :indent t)
                               (:html :xmlns "http://www.w3.org/1999/xhtml"
                                      :xml\:lang "de"
                                      :lang "de"
                                      (:head
                                       (:title ,title)
                                       (:body
                                        (:div :id "wrapper"
                                              (:div :id "header"
                                                    (:h1 "~apb"))
                                              (:div :id "content"
                                                    ,@body)))))))

“(index)”的评估(通过一个测试“(articles)”中的文章返回:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='de' lang='de'>
  <head>
    <title>
      ~apb
    </title>
    <body>
      <div id='wrapper'>
        <div id='header'>
          <h1>
            ~apb
          </h1>
        </div>
        <div id='content'>
          <ul>
            <li>
            </li>
          </ul>
        </div>
      </div>
    </body>
  </head>
</html>

通过查看

  • ..
  • 标签,我想知道为什么没有输出,我认为格式函数有问题。但我不明白是什么。

    I'm trying to build a personal Website via hunchentoot and cl-who, but I'm occurring an semantic error in the following code:

    (defun index ()
      (standart-page (:title "~apb")
                     (dolist (article (articles))
                       (cl-who:htm
                        (:ul
                         (:li (format nil "~a: ~a" (article-date article) (article-title article))))))))
    

    "standart-page" is a macro:

    (defmacro standart-page ((&key title) &body body)   `(cl-who:with-html-output-to-string (*standart-output* nil :prologue t :indent t)
                                   (:html :xmlns "http://www.w3.org/1999/xhtml"
                                          :xml\:lang "de"
                                          :lang "de"
                                          (:head
                                           (:title ,title)
                                           (:body
                                            (:div :id "wrapper"
                                                  (:div :id "header"
                                                        (:h1 "~apb"))
                                                  (:div :id "content"
                                                        ,@body)))))))
    

    The evaluation of "(index)" (with one test article in "(articles)" returns:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='de' lang='de'>
      <head>
        <title>
          ~apb
        </title>
        <body>
          <div id='wrapper'>
            <div id='header'>
              <h1>
                ~apb
              </h1>
            </div>
            <div id='content'>
              <ul>
                <li>
                </li>
              </ul>
            </div>
          </div>
        </body>
      </head>
    </html>
    

    By looking at the <li>..</li> tags I was wondering why there is no output. I think there's something wrong with the format function but I cant't figure out what.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    不奢求什么 2024-10-15 13:55:40

    具体来说,对于格式,安德鲁的答案是正确的。一般来说,您可以使用 str

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:p (str (format nil "~A" '<hello/>))))
    "<p><HELLO/></p>"
    

    请注意,在这种情况下,字符串不是 HTML 转义的(这也适用于 fmt)。如果您希望这样,请使用 esc 代替:

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:p (esc (format nil "~A" '<hello/>))))
    "<p><HELLO/></p>"
    

    同样,在退出 HTML 模式后,使用 htm 返回 HTML 模式:

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:ul (loop for x from 1 to 3
                          do (htm (:li (str x))))))
    "<ul><li>1</li><li>2</li><li>3</li></ul>"
    

    For format specifically, Andrew's answer is the right one. In general, you can use str:

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:p (str (format nil "~A" '<hello/>))))
    "<p><HELLO/></p>"
    

    Note that the string isn't HTML-escaped in this case (this goes for fmt as well). If you want it to be, use esc instead:

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:p (esc (format nil "~A" '<hello/>))))
    "<p><HELLO/></p>"
    

    Similarly, use htm to get back into HTML mode when having escaped out of it:

    CL-USER> (with-html-output-to-string (*standard-output*)
               (:ul (loop for x from 1 to 3
                          do (htm (:li (str x))))))
    "<ul><li>1</li><li>2</li><li>3</li></ul>"
    
    以往的大感动 2024-10-15 13:55:40

    一个明显的错误是你拼错了“标准”。因此,将流绑定到 *standard-output* (原文如此)不会重新绑定 *standard-output*,正如您可能想要的那样。

    One obvious mistake is that you misspelt "standard". Therefore, binding a stream to *standart-output* (sic) does not re-bind *standard-output*, as you perhaps intended.

    瑶笙 2024-10-15 13:55:40

    查看 CL-WHO 网站上的使用示例,我认为您不能只执行返回字符串的格式。他们的所有示例都使用自定义输出函数(例如 fmt),这些函数似乎在后台写入动态变量。在 CL-WHO 生成的代码示例中,您可以将其视为宏扩展中的 (format http-output-stream ....) 行。

    这可以解释为什么您没有得到任何输出,您只需要使用他们的自定义输出编写器而不是您正在使用的 (format nil ..) 。你可能想要类似

    的东西
    (fmt "~a: ~a" (文章-日期文章) (文章-标题文章))

    Looking at the examples of usage on the CL-WHO site I don't think you can just do a format that returns a string. All of their examples either use custom output functions (for example fmt) that appear to write to a dynamic variable under the hood. In the example of code generated by CL-WHO you see this as a (format http-output-stream ....) line in the macro expansion.

    This would explain why you're not getting any output, you just need to use their custom output writers instead of the (format nil ..) that you're using. You probably want something like


    (fmt "~a: ~a" (article-date article) (article-title article))

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文