Skip to content

Custom helpers

Templates may import custom helpers, by specifying the helpers array in the frontmatter. The helpers array contains the relative paths to the helpers, and these paths are relative to the template.

Custom helper files are JavaScript files, often with the .helper.js extension. The contents of this file must be evaluated into a JavaScript function named helpers, that takes no arguments. The return value of this function may contain named helper functions. These helper functions will be registered into Handlebars (the templating engine used by ProJor) and will be available in the template.

This feature is not used very often, but it is available nevertheless. We can only show "synthetic" example of what a custom helper file must look like.

An example helper file looks like this:

javascript
(function helpers() {
    return {
        javaColumnType: function (typeName) {
            if (typeName === "ID") {
                return "Long";
            } else if (typeName === "STRING") {
                return "String";
            }
            return typeName;
        }
    }
})

If you have a template in .projor/template/my-template.ptemplate.mustache, and you place the helper at .projor/helpers/my-helper.helper.js, you should include the following in the template's frontmatter:

json
{
    "helpers": ["../helpers/my-helper.helper.js"]
}

Then you may use it in a template like this:

{
    "helpers": ["../helpers/my-helper.helper.js"],
}
---
public class {{pascalCase name}} {
    {{#each fields}}
    private {{javaColumnType fieldType.name}} {{camelCase name}};
    {{/each}}
}