放大中的范围选择器 [英] Range selectors in enlive
问题描述
我在尝试创建一个范围选择器,并且似乎不能离开。
(sniptest< div>< p class ='start'> Hi< / p>< p class ='end' >有< / p>< / div>
/ pre>
[{[:.start] [:.end]}](contentHello))
这只是返回提供的html。
我如何做到这一点?
EDIT
为了更简洁,这是我使用deftemplate和一个真正的html文件:
< html&
< head>
< title>< / title>
< / head>
< body>
< h1>不是问候< / 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-pagecompojure_blog_test / views / landing_page.html
[blogs]
{[:.start] [:.end]}(html / contentBlah blah ))
我随同本教程,但它使用一个片段来匹配范围。这是否需要?
是否可以用
sniptest
?解决方案这些被称为片段选择器,并且不幸地为了你的目的他们不支持
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'>在< / p>之后< / p>之后有< / p>
< p class = ; / 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 \>之后< / p>
< p class = \end\>后< / p>
< / div>
这可让您根据片段中的位置执行更有趣的操作
user> (html / sniptest< div>
< p class ='before'> before< / p>
< p class ='start'> Hi< / p>
< p class ='end'>在< / p>之后< / p>之后有< / p>
< p class = ; / p>
< / div>
{[:.start] [:.end]}(html / clone-for [m [[HelloSir]]]
[:p.start](html / content(first m))
[:p.end](html / content(last m))))
< div>
< p class = \before\> before< / p>
< p class = \start\> Hello< / p>
< p>之后的< / p>之后的< / p>之后的< / p> $ p
< p class = \\end \>最后< / p>
< / div>
您也可以使用
do->
而不是clone-for
:user& (html / sniptest< div>
< p class ='before'> before< / p>
< p class ='start'> Hi< / p>
< p class ='end'>在< / p>之后< / p>之后有< / p>
< p class = ; / p>
< / div>
{[:.start] [:.end]}(html / do->(html / contentHello)))
< div>
< p class = \before \> before< / p>
< p class = \start\> < / p> $ b之后的< / p>
< p class = \after \>之后" p>
< p class = \end \ $ b< p class = \end\>最后< / p>
< / div>
I'm trying to create a range selector, and can't seem to get off the ground.
I'm trying things like:
(sniptest "<div><p class='start'>Hi</p><p class='end'>There</p></div>" [{[:.start] [:.end]}] (content "Hello"))
And that just returns the supplied html. I'd expect it to return a div with the body "Hello".
How do I do this?
EDIT
Just to be more concise, this is what I've done with deftemplate and a real html file:
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"))
I'm following along with this tutorial, but it uses a snippet to match ranges. Is this neccesary?
Is it possible to test these out with just
sniptest
?解决方案These are called "fragment selectors" in enlive parlance and unfortunatly for your purposes they don't support
content
directly, though if you wrap them in aclone-for
you can get the same effect.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>"
This allows you to do more interesting things based on the position in the fragment
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>"
You can also use
do->
instead ofclone-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>"
这篇关于放大中的范围选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!