title: Scala using JSON and/or XML templates
body: <p>I am using Scalatra, but this question should be valid for any Scala programming. I come from a Ruby on Rails background. Simply put, using templating systems such as XML Builder or jsonbuilder (<a href="https://github.com/rails/jbuilder">https://github.com/rails/jbuilder</a>), I had full control on what my JSON or XML output in an RESTful API would be by creating a template such as the following:</p>
  
  <pre><code>Jbuilder.encode do |json|
    json.content format_content(@message.content)
    json.(@message, :created_at, :updated_at)
  
    json.author do
      json.name @message.creator.name.familiar
      json.email_address @message.creator.email_address_with_name
      json.url url_for(@message.creator, format: :json)
    end
  
    if current_user.admin?
      json.visitors calculate_visitors(@message)
    end
  
    json.comments @message.comments, :content, :created_at
  
    json.attachments @message.attachments do |attachment|
      json.filename attachment.filename
      json.url url_for(attachment)
    end
  end
  </code></pre>
  
  <p>The ideal here is, I put together a <code>@message</code> object with whatever logic is required in a controller+action. That gets passed to a template that has logic such as <code>if current_user.admin?</code> include some stuff, otherwise don't. </p>
  
  <p>What is the equivalent tool that's available in Scala or Scalatra to do something similar? I know a <code>serializer</code> will let me override the JSON or XML that gets generated from a particular model, but that's the same thing in Ruby (correct me if I'm wrong) as overriding <code>as_json</code> or <code>as_xml</code>. Sometimes however, templates are much more complicated and include multiple models, specific structuring of data, specific ordering of data, etc. This is the flexibility I need. Is there current a tool that allows for such templating in a Scala/Scalatra environment?</p>