# Write a template

> Make a template of your own, the package New app turns into an app, with its values, conditions and shared pieces written in Handlebars.

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 {#package}

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

```text
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)
```

```conf file=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:

```conf file=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 {#values}

Templates are written in [Handlebars](https://handlebarsjs.com/guide/). 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 {#conditions}

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

```text file=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 {#partials}

`{{> 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:

```text file=partials/header
// {{App}}, made from ShopTemplate.
```

```text file=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 {#literal}

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

```text file=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 {#limits}

- **Name what nothing defines.** Every `{{…}}`, in every branch of every condition, must be one of section 2's. Anything else stops the template's build, with the file and line.
- **Loop, read files or log.** Handlebars' `each`, `with`, `lookup`, `log` and the others are refused: a template has its parameters, not lists.
- **Reach anything but its values.** A value is only ever its text, never an object to call.
- **Paste a value into a command.** A pipeline check's `command` gets each value as a variable, `${PLINTO_PARAMETER_PORT}`, set in its environment, so a value is never read as more of the command. A webhook check's `url` gets its values percent-encoded.

## 7. Build it and declare it {#declare}

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:

```text
> 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 {#from-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:

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

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