# Templates

> Start projects from the official starters or a template of your own. Write templates others can use.

`kurogane new` generates a project from a template. Every Kurogane template is a [cargo-generate](https://github.com/cargo-generate/cargo-generate) template, the official starters included. A template you write works with `kurogane new` and with plain `cargo generate`.

## Official starters

| Starter | Frontend | Repository |
|---|---|---|
| `minimal` | Vite | [`kurogane-rs/kurogane-starter-minimal`](https://github.com/kurogane-rs/kurogane-starter-minimal) |
| `react` | React and Vite | [`kurogane-rs/kurogane-starter-react`](https://github.com/kurogane-rs/kurogane-starter-react) |
| `svelte` | Svelte 5 and Vite | [`kurogane-rs/kurogane-starter-svelte`](https://github.com/kurogane-rs/kurogane-starter-svelte) |
| `vue` | Vue 3 and Vite | [`kurogane-rs/kurogane-starter-vue`](https://github.com/kurogane-rs/kurogane-starter-vue) |

Each comes in TypeScript and JavaScript. `kurogane new` without arguments asks for a starter, a language and a name. Name them to skip the questions:

```bash
kurogane new react --language javascript --name my-app
```

The CLI fetches each starter from its repository's default branch. The project directory takes the name in kebab-case. Generated projects aren't git repositories and never join a parent Cargo workspace.

## Generate from your own template

`--template` takes a local path, a git URL or a cargo-generate shorthand (`gh:`, `gl:`, `bb:`, `sr:` or `user/repo` for GitHub):

```bash
kurogane new --template gh:user/repo
```

```bash
kurogane new --template ./my-template
```

A starter name and `--template` can't be combined. Private repositories use your git credentials and SSH agent.

Every run fetches a git template again into a snapshot in Kurogane's cache. A run that can't fetch it generates from the last snapshot and says how old it is. `kurogane clean` removes the snapshots.

### Answer placeholders from a file

A template's placeholders are asked for interactively. `--values` takes them from a TOML file instead:

```toml
[values]
language = "javascript"
```

```bash
kurogane new --template gh:user/repo --name my-app --values answers.toml --ci
```

A values file gives the same answers on every run and every machine. `--language` sets the `language` placeholder of any template.

### Hooks

A template can run Rhai hooks during generation. cargo-generate asks before a hook runs a command and shows the command. `--yes` lets hooks run commands without asking. A run with `--ci` and without `--yes` refuses any command a hook asks for.

> [!CAUTION]
> Pass `--yes` only for templates you trust. Hook scripts are never inspected.

The official starters have no hooks.

## Add Kurogane to an existing app

`kurogane init` adds the Rust side to an existing frontend project in the current directory. It writes `Cargo.toml`, `build.rs`, `kurogane.toml` and `src/main.rs`:

```bash
kurogane init --assets dist --dev-url http://localhost:5173
```

* It never overwrites a file. It refuses to run when `kurogane.toml`, `Cargo.toml` or `src/main.rs` already exists.
* It asks for the build output directory and the dev server URL unless `--assets` and `--dev-url` give them. A run with `--ci` needs both.
* `--assets` becomes `frontend-dist` in `kurogane.toml`. `--dev-url ""` makes debug builds load the build output instead of a dev server.

See [Project layout](https://kurogane-rs.org/docs/start/project-layout.md#projects-from-kurogane-init) for the result.

## Write a template

Start from an official starter. Fork it, change it and publish it.

```text
Cargo.toml           # the app crate, depending on kurogane and kurogane-build
build.rs             # calls kurogane_build::build()
kurogane.toml        # the Kurogane manifest
src/main.rs          # the entry point
frontend/            # a self-contained frontend project
cargo-generate.toml  # placeholders and conditional files
```

`cargo-generate.toml` is read during generation and never copied into the project. The other files become the project. See [Project layout](https://kurogane-rs.org/docs/start/project-layout.md) for what each does.

### Placeholders

Every file is rendered with [Liquid](https://shopify.github.io/liquid/). cargo-generate always provides `{{project-name}}` as typed and `{{crate_name}}` in snake case:

```toml
[package]
name = "{{crate_name}}"
```

Declare your own placeholders in `cargo-generate.toml`. One with a default and choices takes its value from `--language`, `--values`, the prompt or its default under `--ci`:

```toml
[placeholders.language]
type = "string"
prompt = "Language"
default = "typescript"
choices = ["typescript", "javascript"]
```

Keep placeholders for real choices. Values every project shares belong in the files as they are.

### Language variants

Keep both languages in one `frontend/` folder. Leave out the files one language doesn't need by condition:

```toml
[conditional.'language == "typescript"']
ignore = ["frontend/main.js"]

[conditional.'language == "javascript"']
ignore = ["frontend/main.ts"]
```

Files both languages share differ inline:

```html
<script type="module" src="/main.{% if language == "typescript" %}ts{% else %}js{% endif %}"></script>
```

That needs no hook. Generating asks for no consent.

### Frontend syntax that looks like Liquid

Wrap your framework's template syntax in `{% raw %}` so Liquid leaves it alone:

```vue
<h1>{% raw %}{{ message }}{% endraw %}</h1>
```

### Pin Kurogane

Pin `kurogane` and `kurogane-build` to the same release. Every generated project then builds the same way:

```toml
[dependencies]
kurogane = "=<version>"

[build-dependencies]
kurogane-build = "=<version>"
```

### Test it

Generate from your working copy once per variant:

```bash
kurogane new --template ./my-template --name check-ts --language typescript --ci
```

```bash
kurogane new --template ./my-template --name check-js --language javascript --ci
```

Then install, build and run each project as a user would.

### Publish it

Push the template to a git host. Users generate from it by its shorthand:

```bash
kurogane new --template gh:you/kurogane-template
```

Generated projects need no setup of their own. `kurogane dev` installs the Chromium runtime when it's missing. A template needs no `.cargo/config.toml` or linker flags for it.
