Closure templates - create a reusable alias for complicated `record` definition

90 Views Asked by At

I have a soy template that looks like

{template .fullView}
  {@param people: list<[age:int, name:string]>}
  {call .headers}
    {param people: $queries /}
  {/call}
  {call .content}
    {param people: $queries /}
  {/call}
{/template}

{template .headers}
  {@param people: list<[age:int, name:string]>}
  # headers
{/template}

{template .content}
  {@param queries: list<[age:int, name:string]>}
  # content
{/template}

As the record definition for "people" has become more complex than just age and name it has become tedious to update the definition of the param in all three places. Is it possible to instead create an alias or something that could be reused in each template?

{alias [age:int, name:string] as Person}
{template .headers}
  {@param people: list<Person>}
  # headers
{/template}
1

There are 1 best solutions below

0
On

Why not define a proto for Person instead? The docs also seems to recommend using protos over records:

In many cases, defining a protocol buffer is superior to using records since it is less verbose.

So you could define a message like this?

// syntax: proto3
message Person {
    int32 age = 1;
    string name = 2;
    // more fields here 
}