Domanda

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?

È stato utile?

Soluzione

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 a clone-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 of 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>"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top