Xml Literals
Viewing old version 6b069ba2f8a308f7d14aeb21128a0ad585e3a5eb; View Current
Scala supports XML literals. Meaning that this program:
outputs this:
This syntax can do more than just save you a few quotes and backslashes, however. You can use curly braces to dynamically insert expressions, as such:
Note how we don’t quote the title or href attributes; Scala knows that we are assigning XML attributes to dynamic variables (i.e. this isn’t just blind templating; it’s XML-aware). Also note how the quoted string we pass to the function gets escaped inside the attribute value.
Inside those curly braces, we can embed any Scala expression we want, so if we wanted to take a list and output it as a ul, this would work just fine:
Notice that inside our expression, we map our list of strings to a list of XML literals, themselves containing a dynamic expression.
Also note that we can put literal curly braces in by either doubling them outside of an attribute, or quoting them as an attribute’s value.
Last Updated 07/25/2009 at 02:23:26 PM by davec
blog comments powered by Disqus
All Content by David Copeland is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.