defsnippet
実際には、Enlive が理解できる任意のソースを使用します。ファイルである必要はありません。特に、link
タグ テンプレートのインライン Enlive スタイル表現にすることができます。バージョン 1.1.0 以降、Enlivenet.grand.enlive-html/html
は Hiccup スタイル表記を解析するヘルパー ( ) も提供します。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
パイプラインの最後に追加すること->>
は、これをテストする便利な方法です。これが出力です(わかりやすくするために2つの改行を手動で挿入しています):
<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />