Skip to content Skip to sidebar Skip to footer

How Do I Display Html Within Html?

Suppose I've got an html document: test And I want to display that code in a browser. Then I'd create something like:
&

Solution 1:

There are a few options:

  1. Roll your own.
  2. Use commons StringEscapeUtils
  3. If you're using hiccup, it comes with a function for this.

For #3, just use the h function in hiccup.core.

For #2, add [org.apache.commons/commons-lang3 "3.1"] to your dependencies, and then you can encode with

(org.apache.commons.lang3.StringEscapeUtils/escapeHtml4 "your string")

For #1, you can use the function hiccup uses. It's pretty small:

(defn escape-html
  "Change special characters into HTML character entities."
  [text]
  (.. ^String (as-str text)
    (replace "&""&")
    (replace "<""&lt;")
    (replace ">""&gt;")
    (replace "\"""&quot;")))

Any of these solutions are fine.

Post a Comment for "How Do I Display Html Within Html?"