Lisp 使用cl who、parenscript和hunchentoot生成内联javascript
我试图生成内联javascript,但我必须使用cl who将parenscript代码放入Lisp 使用cl who、parenscript和hunchentoot生成内联javascript,lisp,common-lisp,hunchentoot,cl-who,parenscript,Lisp,Common Lisp,Hunchentoot,Cl Who,Parenscript,我试图生成内联javascript,但我必须使用cl who将parenscript代码放入(:script)和(str)标记中ps、ps*、ps inline和ps inline*似乎对生成的js没有太大影响 编写宏是为了避免代码重复,还是有更好的方法 以下是我的节目: (in-package #:ps-test) (defmacro standard-page ((&key title) &body body) `(with-html-output-to-string
(:script)
和(str)
标记中ps
、ps*
、ps inline
和ps inline*
似乎对生成的js没有太大影响
编写宏是为了避免代码重复,还是有更好的方法
以下是我的节目:
(in-package #:ps-test)
(defmacro standard-page ((&key title) &body body)
`(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
(:html
:lang "en"
(:head
(:meta :http-equiv "Content-Type"
:content "text/html;charset=utf-8")
(:title ,title)
(:link :type "text/css"
:rel "stylesheet"
:href "/style.css"))
(:body
,@body))))
(defun main ()
(with-html-output (*standard-output* nil :indent t :prologue nil)
(standard-page (:title "Parenscript test")
(:div (str "Hello worldzors"))
(:script :type "text/javascript"
(str (ps (alert "Hello world as well")))))))
(define-easy-handler (docroot :uri "/") ()
(main))
(defun start-ps-test ()
(setf (html-mode) :html5)
(setf *js-string-delimiter* #\")
(start (make-instance 'hunchentoot:easy-acceptor :port 8080)))
(defun stop-ps-test ()
(stop *server*))
(defvar *server* (start-ps-test))
宏在这个用例中很好。 诀窍在于宏按特定顺序展开。说 定义一个
js
宏:当遇到宏扩展时
通过html输出
,对宏的内部调用(js(警报“Ho Ho”)看起来像一个函数调用,在生成的
代码。如果您的js
宏随后扩展为(:script…
),则系统将抱怨:script
是未知函数(假设您
实际上没有这样命名函数)。你应该发出
封闭(who:htm…
表达式以使用
CL-谁是密码行者
(defmacro js (code)
`(who:htm
(:script :type "text/javascript" (who:str (ps:ps ,code)))))
这仅在包含html输出的的上下文中有效
对于内联Javascript,您不希望在其周围有一个
标记,
您通常可以简单地使用ps inline
:
(who:with-html-output (*standard-output*)
(:a :href (ps:ps-inline (void 0))
"A link where the usual HREF behavior is canceled."))
;; prints:
;;
;; <a href='javascript:void(0)'>A link where the usual HREF behavior is canceled.</a>
(谁:具有html输出(*标准输出*)
(:a:href(ps:ps内联(无效0))
“取消通常HREF行为的链接。”)
;; 印刷品:
;;
;;
但如果您经常做同样的事情,请随意使用宏:
(defmacro link (&body body)
`(who:htm (:a :href #.(ps:ps-inline (void 0)) ,@body)))
(who:with-html-output (*standard-output*) (link "Link"))
;; prints:
;;
;; <a href='javascript:void(0)'>Link</a>
(定义宏链接(&body)
`(who:htm(:a:href#(ps:ps内联(void 0)),@body)))
(世卫组织:html输出(*标准输出*)(链接“链接”))
;; 印刷品:
;;
;;