Skip to content

Conditional templates

Conditional templates are a special subcase of forEach templates, and map templates ...

  • In case of forEach - They will filter out data objects based on a condition you provide, and only generate files for the objects that pass the condition. You can also use $ to access globals in the condition.
  • In case of map - They will filter out the entire generated file, if the condition fails. You can use the same mapped data collections, and $ as in the template itself.

Template conditions are defined on the first line of the template file, using JavaScript code between @if and @endif. You can use multiple lines for the condition, but it must start on the first line.

The condition JavaScript expression must evaluate to an arrow function, that takes a single parameter, called data, and returns a boolean value. This parameter will contain the data object that is being processed.

For example to only generate a .cs files for entities, if noGenerateEntityClass is not set to true:

@if data => data.noGenerateEntityClass !== true @endif
{
    "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}}
    }
}

An example for a map template, that will only generate a client.ts file, if noClient in .pglobal.yaml files is not set to true:

@if data => data.$.noClient !== true @endif
{
    "map": { "entities": "entities" },
    "filename": "client.ts"
}
---
{{#each entities}}
export interface {{pascalCase name}} {
}
{{/each}}