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?
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.