Skip to content

Infix templates

Infix templates are defined by setting the infix field in the template frontmatter:

{
    "map": {
        "entities": "entities"
    },
    "infix": {
        "start": "/// Begin generated code\n",
        "end": "\n/// End generated code"
    },
    "filename": "packages/entities/src/index.ts"
}
---
{{#each entities}}
export interface I{{pascalCase name}} {
    {{#each fields}}
    {{pascalCase name}}: {{type.ts}};
    {{/each}}
}
{{/each}}

If you had this template, it would do the following:

  • First, ProJor reads the packages/entities/src/index.ts file.
  • Then, it looks for the infix start and end strings in the file.
  • Then, it renders the template content.
  • Finally, it inserts the rendered content between the start and end strings.

At the end of the code generation, the modified file will be written back to the disk.

You can also use multiple infixes on the same file, but execution order of these templates is not guaranteed (shouldn't be an issue).

IMPORTANT NOTE! Beware of the file's line endings when using infix templates. ProJor will always output \n (LF) line endings, regardless of the original file's line endings, but incorrect line endings may not match the exact infix string, which causes a file generation error.