Open plinto
Docs›Guides

Write a template

Guide·10 min read

A template is a package that New app, in the Templates app, makes apps from: from one name, a package with its code, its version group, its environments and its pipeline. You write it once; every app made from it starts the same way.

1. The package

A template is a folder with a plinto.conf that the Template build system builds:

ShopTemplate/
├── plinto.conf      build = Template
├── template.conf    what an app made from it gets
├── README.md        what someone picking it reads
├── files/           the new package's files
└── partials/        pieces the files share (if any)
plinto.conf
name = ShopTemplate
version = 1.0
build = Template

build-dependencies: {
  Template = 2.0
}

Name it after what it makes, ending in Template. template.conf says the parameters New app asks for besides the app's name, and what the app gets:

template.conf
{
  description = "A Go web service on a host group of yours"
  parameters: {
    hosts: { label = "Runs on", kind = host-group }
    port: { label = "Port", kind = port, default = "8080" }
  }
  package: { name = "{{App}}" }
  version-group: { name = "{{app}}" }
  environments = [
    { path = "{{App}}" }
    { path = "{{App}}/Prod", targets = ["{{hosts}}"], config: { PORT = "{{port}}" } }
  ]
}

A parameter's kind is text (the default), java-package, host-group (one of your groups, with a ready host) or port (free on its host groups). Quote a value with {{ in it: in template.conf, } ends a bare value.

2. Values

Templates are written in Handlebars. Each {{…}} is filled in, the same in the files' paths, their contents and template.conf's values:

You write For an app named hello-web
{{App}} the name as typed: hello-web
{{app}} in lower case
{{port}}, any parameter its value
{{App.class}} as a class name: HelloWeb
{{App.package}} as a Java package: hello.web
{{java-package.path}} a Java package as folders: com/example/hello/web
{{App.lower}} in lower case
{{port.systemd}} user for a port from 1024 up, system below
{{version.Go}} what the app's version group pins of Go, as plinto.conf writes it: 1.27

The forms work on any value: {{java-package.path}}, {{App.class}}. A path can use them too, files/src/{{java-package.path}}/{{App.class}}Application.java. The packages a template names with {{version.X}} are what it needs: New app checks that your version group pins each, or that the template imports it. A package whose name has a dot is written {{version.[Name0.5]}}.

3. Conditions

{{#if}}, {{else}} and {{#unless}} put text in or leave it out, with eq, neq, and, or and not:

files/README.md
{{App}} listens on {{port}}.
{{#if (eq port.systemd "system")}}
Below 1024, so it runs as a system service: its host group must take them.
{{/if}}

A block alone on its line takes its line with it, so the README above has no empty lines where the condition was. A file whose path is filled in to nothing isn't made: files/{{#if (eq port.systemd "system")}}SYSTEM.md{{/if}} is there only for ports below 1024.

4. Shared pieces

{{> header}} writes the file partials/header, filled in like the rest. A piece alone on its line takes the line, so end the file with a newline:

partials/header
// {{App}}, made from ShopTemplate.
files/src/main.go
{{> header}}
package main

A piece used by one file only can be written in it: {{#*inline "greeting"}}Hello from {{App}}{{/inline}}, then {{> greeting}}.

5. Writing {{ itself

\{{ writes {{ and leaves what follows as it is, for a file that has its own braces:

files/.github/workflows/build.yml
env:
  TOKEN: $\{{ secrets.PLINTO_TOKEN }}
  APP: {{App}}

In a quoted template.conf value, write \\{{: the conf reads one backslash itself. {{!-- … --}} is a comment the app doesn't get, and {{~ or ~}} trims the space next to it.

6. What a template can't do

7. Build it and declare it

Release the template into the version group you keep templates in. Its build checks template.conf, makes an app named Sample from it into build/sample/, with each parameter's default, and says what it needs:

> check
needs Go, GoBuild; made the sample app in build/sample
> archive

Only the defaults' branch of a condition is made: the other branch's names are checked, and New app makes it with the maker's values before anything is created. Then, in the Templates app, Declare a template: name the package and the group. New app offers it from then on.

From Template 1.0

A template built with Template = 1.0 keeps making apps as it did. To move it, say Template = 2.0 and write {{version.X}} where it says {{version X}}. That is the only spelling that changed, and the build names each one it finds:

files/plinto.conf: {{version Go}} is Template 1.0's; write {{version.Go}}

Template 1.0 is deprecated: its builds warn, and nothing stops.