defsnippetはhtmlの特定の部分にのみ一致します(これがセレクタを引数として使用する理由です)。それを変換します。 deftemplateはhtml全体を取り込み、それを変換します。また、defsnippetはClojureデータ構造体を返しますが、deftemplatesは文字列のベクトルを返します。したがって、通常、デフォニペットはデフテンプレート内で使用されます。
header.html:
あなたのような何かをしたいあなたのケースでは
(enlive/html-snippet "<div id='foo'><p>Hello there</p></div>")
;=({:tag :div, :attrs {:id "foo"}, :content ({:tag :p, :attrs nil, :content ("Hello there")})})
:あなたのデータは、スニペット(またはセレクタ)によって返されたかのアイデアを与えることを
がどのように見えます
<div id="my-header-root">
...
</div>
Clojureのコード:
(enlive/defsnippet header "path/to/header.html" [:#my-header-root] []
identity)
(enlive/defsnippet footer "path/to/footer.html" [enlive/root] []
identity)
(enlive/deftemplate layout "layout.html" [header footer]
[:head] (enlive/content header)
[:body] (enlive/append footer))
(defroutes home-routes
(GET "/" [] (layout (header) (footer))
スニペットで使用されるidentity関数は、この場合、#my-header-rootセレクタによって選択されたデータ構造です(変換は行いません)。つまりhead.htmlにすべてを含める場合は、ルートセレクタを使用できます。
あなたはこのようなものを使用してdefsnippetから生成されたHTMLを表示することができます:https://github.com/swannodette/enlive-tutorial/ とどのようにdefsnippetとdeftemplateマクロのいくつかの詳細は、ブライアン・マリカずつ:
(print (apply str (enlive/emit* (my-snippet))))
を私はまた、チュートリアルをお勧めします作業。
(enlive/sniptest "<p>Replace me</p>"
[:p] (enlive/content "Hello world!"))
;= "<p>Hello world!</p>"
ありがとう:
最終先端には、セレクタや変換がenliveが付属していますsniptestマクロを使って実験することができます!将来の読者のための訂正。この行は、セレクタの後ろにベクトルを持つ必要があります(拡張/ defsnippetヘッダ "path/to/header.html" [:#my-header-root] アイデンティティ)。だからそれは:(拡張/ defsnippetヘッダ "path/to/header.html" [:#my-header-root] [] identity)。フッタのdefsnippetと同じです。 –
ああ、それを逃した。私はそれに応じて例を更新しました。 – ebaxt