replace-vars in enlive 1.1.4

407 views Asked by At

I have the following html that is loaded into a template...

Welcome <b id="alert-username">${fullname}</b>

I have the following selector/action in a template...

[:#alert-username] (replace-vars {:fullname (fullname request)})

This is rendered as follows...

 Welcome ${fullname}

Which is obviously not what I'm looking for.

However, it works if I do it the hard way...

[:#alert-username] (fn [n] (assoc n :content [(fullname request)]))

which yields...

Welcome Bill

So I know that it's not an issue getting the username from the request because the above code does what it should.

What am I doing wrong here?

2

There are 2 answers

3
Jared314 On BEST ANSWER

The replace-vars transformation does not recurse into child nodes, including text content. The selector :#alert-username selects the <b id="alert-username"> tag:

{:tag :b, :attrs {:id "alert-username"}, :content ["${fullname}"]}

So, replace-vars, because it is used on the tag, will search the the tag's attributes and ignore the tag's contents.

You can apply the transform to the contents with something like the following:

[:#alert-username] (transform-content
                     (replace-vars {:fullname (fullname request)}))

But, that will also search any child tag attributes, along with any child text nodes.

Note: The transform-content macro is part of the net.cgrand.enlive-html namespace.

0
claj On

Since you have a tag enclosing the ${fullname}, you could just use the transform

[:#alter-username] (content (fullname request))

and put any text in the tag. The content in of the tag given in the the template will be replaced anyway.