在
Ruby中我可以这样:
string=<<EOTEMPLATE <root> <hello> <to>%s</to> <message>welcome mr %s</message> </hello> ... </root> EOTEMPLATE
当我想“渲染”模板时,我会这样做:
rendered = string % ["me@mail.com","Anderson"]
它将使用数组中传递的值填充模板.有没有办法在Scala中执行此操作,而不是使用Java的String.format?如果我在Scala中写这个:
val myStr = <root> <hello> <to>{address}</to> <message>{message}</message> </hello> </root>
生成的XML已经“填充”了.有没有办法可以“模板化”XML?
使用函数和Scala的XML:
val tmpl = {(address: String,message: String) => <root> <hello> <to>{address}</to> <message>{message}</message> </hello> </root> }
和:
tmpl("me@mail.com","Anderson")
一些糖:
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 % ("me@mail.com","Anderson")