Nitesh Virani Nitesh Virani - 7 months ago 68
Scala Question

Generate dynamic HTML file

I am new to Scala. I have predefined html file. I want to plug dynamic content in HTML file.

$for(products in product)

is Scala collection. I can load html file and feed dynamic values(

Would you advise me how to do this in Scala?


Twirl templates

compiled, typesafe, composable, scala inside Html tags (akin angular js [javscript inside Html])

Twirl templates enable you to write scala inside a html, its typesafe, gets compiled to functions and are composable as well.


@(customer: Customer, orders: List[Order])


@for(order <- orders) {

Without any library

Scala has good support for string interpolation and also provides triple quote syntax for declaring strings with espace characters which is very handy. You can produce dynamic Html using this simple method

 case class Product(name: String)

  case class Html(content: String)

  def produceHtml(products: List[Product]): Html = {
    Html { { product =>
           |<div> ${} </div>