How to prevent HTML escaping in Clojure Reagent (Hiccup like)
Asked Answered
P

1

6

I am using ClojureScript Reagent. Which provides hiccup-like HTML generation.

I have a String with HTML:

(def code "<b>hello world</b>")

When passed to Hiccup it will be escaped and I get no bold text on my page:

[:div code]

How to pass code to my HTML output so it will be integrated there without being escaped?

Perfective answered 21/9, 2016 at 17:1 Comment(0)
C
15

Reagent

Use the dangerouslysetInnerHTML native React call

[:div {:dangerouslySetInnerHTML {:__html code}}])

Also see:

(real) Hiccup

You need to use the raw-string function from hiccup.utils:

[:div (raw-string code)]
Criminology answered 21/9, 2016 at 17:6 Comment(4)
Thank you very much. But I use reagent which is hiccup-like but not realy hiccup. Therefor there is no hiccup.util namespace :-(Perfective
You have not. I have changed my question. Sorry ;-)Perfective
@Perfective That's OK, next time, if you noticed that you have an edit that would invalidate existing answers, please ask a new question, linking to the old one, with explaining the difference.Batfowl
It looks like there is no longer a raw-string function in the hiccup.util namespace: weavejester.github.io/hiccup/hiccup.util.html Edit: looks like it's part of v2: cljdoc.org/d/hiccup/hiccup/2.0.0-alpha2/api/…Scyphate

© 2022 - 2024 — McMap. All rights reserved.