0

I'm printing out a map's key values to html, and the key namespaces are vanishing, which I don't want.

layout below calls hiccup's html5 to render:

(layout (str "Path " (:path/title path))
  [:h1 "Title: " (:title path) slug]
  [:p (str path)]       ; prints "{:db/id 17592186045542, :path/title "sdf"}"
  (println (keys path)) ; prints in terminal "(:db/id :path/title)"
  [:p (keys path)]      ; prints "idtitle" 
  (for [[k v] path] [:p k " " v]) ; prints "id 17592186045542" /n "title sdf" 
  (map (fn [[k v]] [:p k " " v]) path)))) ; same as above

In both (keys path), and the for & map calls, the ":db/" and ":path/" namespaces of the keys are not rendered. Why?

mwal
  • 2,803
  • 26
  • 34
  • 1
    I suppose the keys are being implicitly `name`d, unlike the good cases where you explicitly use `str` on them. Maybe you should use `[:p (str k) " " (str v)]` or simply `[:p (str/join " " [k v])]` – Reut Sharabani Aug 09 '20 at 07:39
  • yes, that works :) So now I'm wondering what makes an implicit call to `name` a feature, rather than a bug... – mwal Aug 09 '20 at 07:51
  • My guess is either way isn't perfect, but naming the key feels more general to me since outside the application context the namespace is usually irrelevant – Reut Sharabani Aug 09 '20 at 07:53

1 Answers1

2

I suppose the keys are being implicitly named, unlike the good cases where you explicitly use str on them.

Maybe you should use

[:p (str k) " " (str v)]

Or simply:

[:p (str/join " " [k v])]
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • 1
    To check I'm clear on what we are doing here and why this solution works - we are converting a keyword to a string in order that the hiccup macro does not see a keyword and think "oh, i'll call `name` on that to be helpful". So essentially we are adding a workaround to prevent the macro doing something we don't want (a far from ideal situation, but I fully volunteer that this use case of printing out namespaced keys is really just me exploring/constructing a debugging tool, and not a 'real' use case, hence it would not merit being considered an issue with hiccup at this stage.). – mwal Aug 09 '20 at 08:08
  • 1
    Or `(str k " " v)` – cfrick Aug 09 '20 at 13:38
  • I like yours the most :) :) – Reut Sharabani Aug 09 '20 at 13:44