我正在尝试创建一个范围选择器,似乎无法开始。
我正在尝试这样的事情:
(sniptest "<div><p class='start'>Hi</p><p class='end'>There</p></div>"
[{[:.start] [:.end]}] (content "Hello"))
然后只返回提供的html。我希望它能以身体“Hello”返回一个div。
我该怎么做?
修改
为了更简洁,这是我用deftemplate和一个真正的html文件做的:
HTML 的
<html>
<head>
<title></title>
</head>
<body>
<h1>Not hello</h1>
<div class="start">
foo
</div>
<div class="end">
bar
</div>
</body>
</html>
CLJ
(ns compojure-blog-test.views.landing-page
(:require [net.cgrand.enlive-html :as html]))
(html/deftemplate landing-page "compojure_blog_test/views/landing_page.html"
[blogs]
{[:.start] [:.end]} (html/content "Blah blah"))
我正在关注this tutorial,但它使用一个代码段来匹配范围。这是必要的吗?
是否可以仅使用sniptest
来测试这些?
答案 0 :(得分:3)
这些被称为“片段选择器”,用于生活用语并且不幸地为了您的目的而不直接支持content
,但如果将它们包裹在clone-for
中,您可以获得相同的效果。
user> (require '[net.cgrand.enlive-html :as html])
nil
user> (html/sniptest "<div>
<p class='before'>before</p>
<p class='start'>Hi</p>
<p class='end'>There</p>
<p class='after'>after</p>
<p class='end'>last</p>
</div>"
{[:.start] [:.end]} (html/clone-for [m ["Hello"]]
[:p] (html/content m)))
"<div>
<p class=\"before\">before</p>
<p class=\"start\">Hello</p>
<p class=\"end\">Hello</p>
<p class=\"after\">after</p>
<p class=\"end\">last</p>
</div>"
这允许你根据片段中的位置做更多有趣的事情
user> (html/sniptest "<div>
<p class='before'>before</p>
<p class='start'>Hi</p>
<p class='end'>There</p>
<p class='after'>after</p>
<p class='end'>last</p>
</div>"
{[:.start] [:.end]} (html/clone-for [m [["Hello" "Sir"]]]
[:p.start] (html/content (first m))
[:p.end] (html/content (last m))))
"<div>
<p class=\"before\">before</p>
<p class=\"start\">Hello</p>
<p class=\"end\">Sir</p>
<p class=\"after\">after</p>
<p class=\"end\">last</p>
</div>"
您也可以使用do->
代替clone-for
:
user> (html/sniptest "<div>
<p class='before'>before</p>
<p class='start'>Hi</p>
<p class='end'>There</p>
<p class='after'>after</p>
<p class='end'>last</p>
</div>"
{[:.start] [:.end]} (html/do-> (html/content "Hello")))
"<div>
<p class=\"before\">before</p>
<p class=\"start\">Hello</p>
<p class=\"end\">Hello</p>
<p class=\"after\">after</p>
<p class=\"end\">last</p>
</div>"