In Ruby I could have this:
string=<<EOTEMPLATE
<root>
<hello>
<to>%s</to>
<message>welcome mr %s</message>
</hello>
...
</root>
EOTEMPLATE
And when I want to "render" the template, I would do this:
rendered = string % ["[email protected]","Anderson"]
And it would fill the template with the values passed in the array. Is there a way to do this in Scala, other than using Java's String.format
? If I write this in Scala:
val myStr = <root>
<hello>
<to>{address}</to>
<message>{message}</message>
</hello>
</root>
the resulting XML would already be "filled". Is there a way I could "templatize" the XML?
Using a function and Scala's XML:
val tmpl = {(address: String, message: String) =>
<root>
<hello>
<to>{address}</to>
<message>{message}</message>
</hello>
</root>
}
and:
tmpl("[email protected]","Anderson")
Some sugar:
def tmpl(f: Product => Elem) = new {
def %(args: Product) = f(args)
}
val t = tmpl{case (address, message) =>
<root>
<hello>
<to>{address}</to>
<message>{message}</message>
</hello>
</root>
}
t % ("[email protected]","Anderson")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With