user1086138
user1086138

Reputation: 21

snippet does not interpret html tags

I need is to display the contents of the list in a snippet.

I have a function like this:

  def generateHtml(data: List[Documents]): NodeSeq = {
    val html = <ul><li></li></ul>
    def  css = "li" #> data.map {
      document =>
        "* *" #>  ( document.title + ": " + document.content )
    }
    css(html)
  }

List values ​​have html code like this:

val data: List[Document] = List(Document("<b>title</b> 1", "content 1"),Document("`<b>title</b> 2", "content 2") )

works well because it shows me the list values, but the problem is that it does not interpret the html code (labels <b>)

in my snippet, it shows me something like this:

<b>title</b> 1: content 1
<b>title</b> 2: content 2

but what I need is to interpret the tas b

something like this:

title 1: content 1

title 2: content 2

any suggestion that I can do to interpret the tags

I found a similar problem here: Scala: Parse HTML-fragment

probe with the solutions, but do not work

Upvotes: 0

Views: 228

Answers (2)

VasiliNovikov
VasiliNovikov

Reputation: 10256

Adding to what jcern said, you'd better keep the titles without the <b></b> inside. And while rendering, you could write

"li" #> data.map(doc => <li><b>{doc.title}</b>: {dot.content}</li> )

alternatevly, and even more better, you can use "CSS transformations" like that:

val html = <ul><li> <b class="title">t</b>: <span class="content">c</span> </li></ul>
// (in real-world render method, the html: NodeSeq is taken as a method parameter)

val transformation = "ul *" #> data.map{ doc =>
    ".title *" #> doc.title & 
        ".content" #> doc.content
}

transformation(css)

Upvotes: 0

jcern
jcern

Reputation: 7848

This happens because there is a conversion between String to scala.xml.Text, which escapes characters by default. If you wrap the String in scala.xml.Unparsed, it should do what uou are looking for:

def generateHtml(data: List[Documents]): NodeSeq = {
  val html = <ul><li></li></ul>
  def  css = "li" #> data.map {
    document =>
      "* *" #>  scala.xml.Unparsed( document.title + ": " + document.content )
  }
  css(html)
}

Note that is is not a great idea to do with untrusted content, ie: stuff that a user may enter. In those situations, you would probably be better off using something like Markdown or Textile.

Upvotes: 1

Related Questions