我正在尝试创建一个范围选择器,但似乎无法启动。
我正在尝试这样的事情:
(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
来测试这些?
最佳答案
这些在 enlive 中被称为“片段选择器”,不幸的是,对于您的目的来说,它们不直接支持 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>"
关于clojure - enlive 中的范围选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17157780/