defsnippet
实际上采用 Enlive 可以理解的任何来源,它不需要是文件。特别是,它可以是link
标签模板的内联 Enlive 样式表示。从 1.1.0 版本开始,Enlive 还提供了一个帮助器 ( net.grand.enlive-html/html
) 来解析 Hiccup 样式符号;我发现打嗝式手写更方便,所以我将在下面使用它。(您还可以通过将内联 HTML 字符串包装在StringReader
:中来使用它们(java.io.StringReader. "<div></div>")
。)
这是代码:
(require '[net.cgrand.enlive-html :as enlive])
(enlive/defsnippet link-css
;; representation of the link tag template:
(enlive/html [:link {:href "" :rel "stylesheet"}])
;; selector is required, :link works fine here:
[:link]
;; this is the parameter vector of the fn defsnippet will generate:
[hrefs]
;; clone-for will generate one link tag for each provided href:
(enlive/clone-for [href hrefs]
[:link]
(enlive/set-attr :href href)))
你会像这样使用它:
(->> (link-css ["css/base.css" "css/index.css" "css/more_css.css"])
(enlive/emit*)
(apply str))
;= "<link href=\"css/base.css\" rel=\"stylesheet\" /><link href=\"css/index.css\" rel=\"stylesheet\" /><link href=\"css/more_css.css\" rel=\"stylesheet\" />"
添加println
到->>
管道的末尾是一种方便的测试方法;这是输出(为清楚起见,手动插入了两个换行符):
<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />