type: page
Basic syntax
Every .ptemplate.mustache
file has the following structure:
FRONTMATTER
---
CONTENT
The FRONTMATTER
is JSON object that contains metadata about the template. The CONTENT
is the actual template content, written in the Mustache templating language.
Frontmatter
The frontmatter defines:
- Where the template gets its data from - you can use different types of operators (forEach, map) to build the template's context. Globals are also always merged into this context, under the key
$
. filename
- This is the name of the generated file (or files). It is also a Mustache template, that is rendered with the same context as the template itself.- Optionally, the template may indicate that it is an infix template, by setting the
infix
field to an object of{"start": "string", "end": "string", "join": "string" | null}
. - Optionally, the template may be a conditional template, by prefixing the JSON object with
@if CONDITION @endif
. - Optionally, the template may import one or more [helpers](/overview/custom-helpers), by listing their relative paths to the template in the
helpers
array. - Optionally, the template may indicate that it is a once template, by setting the
once
field totrue
.
An example frontmatter looks like this:
{
"forEach": "services",
"filename": "src/main/java/{{pathCase $.group}}/services/{{pathCase name}}Service.java"
}
Content
The content of the template is written in the Mustache templating language. Depending on the operator of the template, it is rendered with different contexts.
In this part, we will introduce some basic, and most-often-required parts of the Mustache templating language, used with real-world-like data.
- You can use
{{#each}}
to iterate over an array. You must terminate the looped section with{{/each}}
. - You can use
{{#if}}
to conditionally render a section. You must terminate the conditional section with{{/if}}
. - You can use
{{#unless}}
to conditionally render a section if the condition is false. The best pattern to do if-else is to use an{{#if}}
and an{{#unless}}
together, with the same condition. You must terminate the conditional section with{{/unless}}
. - Mustache is very context sensitive! If you use
{{#each}}
or{{#with}}
, and you want to access variables from the parent context, you must use{{../}}
to access them.
{
"forEach": "services",
"filename": "src/main/java/{{pathCase $.group}}/services/{{pathCase name}}Service.java"
}
package {{$.group}}.services;
/**
* {{{description}}}
*/
public interface {{pascalCase name}}Service {
{{#each operations}}
public {{returnType.java}} {{camelCase name}}(
{{#each parameters}}
/**
* {{{description}}}
*/
{{#if type.class}}
{{dotCase ../$.group}}.model.{{pascalCase type.class.name}} {{camelCase name}}{{#unless @last}}, {{/unless}}
{{/if}}
{{#unless type.class}}
{{type.java}} {{camelCase name}}{{#unless @last}}, {{/unless}}
{{/unless}}
{{/each}}
);
{{/each}}
}
Important note about {{
! If you want to include text in the template, that contains UNSAFE CHARACTERS FOR HTML (e.g. <
), you must use {{{fieldName}}}
(tripple-curly-braces)!