0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.