This is a bit of an obscure question, but what would be the best way to loop within Scala's string interpolation? For instance, if you wanted to do this
html"""<ul>
${
for (todoItem <- todoList) {
html"""<li>TODO: ${todoItem}</li>"""
}
}
</ul>"""
I see no concise way to accumulate the inner html classes so that the string interpolator could use it. The only thing I can think of is
html"""<ul>
${
var htmlList=List.empty[Html]
for (todoItem <- todoList) {
htmlList :+ html"""<li>TODO: ${todoItem}</li>"""
}
htmlList
}
</ul>"""
and adding support for it in my custom html interpolator
It doesn't make any difference whether you're working within string interpolation or not. That is actually the point of string interpolation: within a code block you have all the power and features of the language available to you, just like it was any other block.
You should write this in functional style, which you can do with yield
in a for
loop, or a map on a list:
html"""<ul>
${
for (todoItem <- todoList) yield html"""<li>TODO: $todoItem</li>"""
}
</ul>"""
html"""<ul>
${
todoList.map{ todoItem => html"""<li> ODO: $todoItem</li>""" }
}
</ul>"""