Frage

Here's the thing with enlive 1.1.5 (source formatting/whitespace changes added for clarity.):

blogen.core> (html/sniptest "<html><head>
                                <title><span id=\"foo\"/></title>
                              </head></html>"
                            [:#foo] (html/substitute "f"))
"<html><head><title></title></head></html>f"

blogen.core> (html/sniptest "<html><head>
                                <title><span id=\"foo\"/></title>
                              </head></html>"
                            [:#foo] (constantly "f"))
"<html><head><title></title></head></html>f"

I want to write a constant prefix in the HTML sources so that my clojure code doesn't saturate from my end content. But as the sniptest above shows, I can't have span tags inside title. The second test using core function constantly shows that any more custom-written transformation is not likely to succeed any better.

I wouldn't like to use the ${vars} because they look stupid in the templates. I prefer to write decent examples in the templates that enlive can then substitute without damage.

Motivation

I would enjoy writing the templates as HTML and use span elements with defined id's as variable placeholders, basically. But enlive doesn't parse those span tags inside title's as desired. To keep things less complected. Examples:

<title><span id="article-name"/> - <span id="my-site" /></title>

or

<p>Welcome, <span id="visitor-ip" /></p>
War es hilfreich?

Lösung

According to the HTML5 spec, the <title> tag's ContentModel is Text, which means that HTML elements are not allowed within it.

The part of Enlive that parses the HTML can probably only handle valid HTML, so it won't play nicely with the <span> tag inside the <title> tag.

You've got a few options.

It is possible to just set the content of the <title> tag rather than substituting it completely, like this:

(html/sniptest "<html><head><title>Placeholder</title></head></html>"
               [:title] (html/content "foo"))

;; => <title>foo</title>

Or if you want to keep a part of the title constant, you could use append or prepend

(html/sniptest "<html><head><title>Some Title</title></head></html>"
                [:title] (html/append " - sub page"))

;; => <title>Some Title - sub page</title>

EDIT

I know that you said you want to avoid ${vars}, but in this case, they happen to do exactly what you're looking for...

(html/sniptest "<html><head><title>${my-site} - ${article-name}</title></head></html>"
               [:title] (html/transform-content
                          (html/replace-vars
                             {:article-name "Using Enlive for good and evil"
                              :my-site "Clojure Weekly"})))

;; =>  <title>Clojure Weekly - Using Enlive for good and evil</title>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top