10.10.3 JSON View Templates

You can define templates starting with underscore _. For example given the following template called _person.gson:

  1. model {
  2. Person person
  3. }
  4. json {
  5. name person.name
  6. age person.age
  7. }

You can render it with a view as follows:

  1. model {
  2. Family family
  3. }
  4. json {
  5. name family.father.name
  6. age family.father.age
  7. oldestChild g.render(template:"person", model:[person: family.children.max { Person p -> p.age } ])
  8. children g.render(template:"person", collection: family.children, var:'person')
  9. }

Alternatively for a more concise way to invoke templates, using the tmpl variable:

  1. model {
  2. Family family
  3. }
  4. json {
  5. name family.father.name
  6. age family.father.age
  7. oldestChild tmpl.person( family.children.max { Person p -> p.age } ] )
  8. children tmpl.person( family.children )
  9. }