Skip to content

forEach templates

You can use forEach: "data-collection-id" or forEach: ["first-data-collection-id", "second-data-collection-id"] to iterate over all data objects in a data collection or multiple data collections. This will create one file for each data object.

In the filename frontmatter field, the render context will contain each field of the data object. The same applies for the content of the template.

Example forEach template:

{
    "forEach": "entities",
    "filename": "Entities/{{pascalCase name}}.cs"
}
---
using System;

namespace Entities
{
    /** {{{description}}} */
    public class {{pascalCase name}}
    {
        {{#each fields}}
        /** {{{description}}} */
        public {{type.cs}} {{pascalCase name}} { get; set; }
        {{/each}}
    }
}
When to use arrays?

Defining forEach as an array comes in handy, when you have multiple data collections sharing the same schema.

One particular example is if you have a query that produces some objects, and also a data collection that adds "manual instances" of the same schema.

Then you would say:

{
    "forEach": ["generated-entities", "entities"],
    "filename": "Entities/{{pascalCase name}}.cs"
}
---
... template content ...