# Write reports from templates

The station computes; a template next to its handler lays the result out as markdown. `renderLocal` reads the template from the station's folder and fills it in with [EJS](https://ejs.co).

```text
src/stations/IN1__check_invoices/
├── station.js
├── report.md                    the run report
├── totals-check.md              a document: attached, or written into the workpiece
└── read-invoice.prompt.md       a prompt for a model
```

| File | Holds | Goes to |
|---|---|---|
| `report.md` | The run report | `attachReport` |
| `<name>.md` | A document, named for what it shows (`totals-check.md`) | `attachDocument`, or a file in the workpiece |
| `<name>.prompt.md` | A prompt, read by a model rather than a person | Your model call |

## Render a template

```js
const report = renderLocal(import.meta.url, './report.md', { checked, passed, held, failed, formatAmount });
await attachReport(work_record.id, report);
```

`import.meta.url` makes the path relative to `station.js`, wherever the factory runs from. The template sees only the data you pass: there are no built-in helpers, so pass any formatting function, such as an amount formatter, in the data.

## A report template

The body of `IN1`'s `report.md`, below its title:

```ejs
<% if (checked === 0) { -%>
No invoices waiting in `IN1/input`.
<% } else { -%>
| Checked | Passed | Held | Failed |
|--------:|-------:|-----:|-------:|
| <%= checked %> | <%= passed %> | <%= held.length %> | <%= failed.length %> |

<% if (held.length > 0) { -%>
## Held

| Workpiece | Vendor | Total | Why |
|-----------|--------|------:|-----|
<% for (const h of held) { -%>
| `<%= h.workpiece_id %>` | <%= h.vendor ?? '—' %> | <%= formatAmount(h.total) %> | <%= h.reason %> |
<% } -%>
<% } -%>
<% } -%>
```

It follows [A report someone can act on](/docs/method/run-report): the counts first, then what needs attention, numbers aligned right, `—` for a missing value, and a line for an empty run.

## End control lines with `-%>`

A line that holds only EJS control (`if`, `for`, `} else {`, a closing `}`) leaves an empty line behind when it ends in `%>`. Inside a markdown table, an empty line ends the table, and the rows after it print as plain text. End those lines with `-%>`, which drops the line break:

```ejs
<% for (const h of held) { %>        ← leaves a blank line between rows
<% for (const h of held) { -%>       ← doesn't
```

## Escaped or raw

| Tag | Output | Use for |
|---|---|---|
| `<%= value %>` | HTML-escaped | Values in documents people read |
| `<%- value %>` | As is | Markdown the station built (a bold verdict line), and everything in a prompt |

In a `.prompt.md`, always use `<%-`. A prompt is plain text for a model: `<%=` would turn a JSON schema's quotes into `&#34;` and an email's apostrophes into `&#39;`, and the model would read the garbled version.
