# prettier-plugin-go-template
[](https://www.npmjs.com/package/prettier-plugin-go-template) [](https://codecov.io/gh/NiklasPor/prettier-plugin-go-template) [](#contributors-)
Formatter plugin for go template files. The only peer dependency is [prettier](https://www.npmjs.com/package/prettier). Test you own code on the [**playground**](https://prettier-plugin-go-template-playground.niklaspor.dev/).
```bash
npm install --save-dev prettier prettier-plugin-go-template
```
The following file types are detected automatically:
`.gohtml`, `.gotmpl`, `.go.tmpl`, `.tmpl`, `.tpl`, `.html.tmpl`
Input |
Output |
```html
{{ if or .Prev .Next -}}
{{ $p := where site.Pages }}
{{ with $p.Next . -}}
{{ end -}}
{{ end -}}
```
|
```html
{{ if or .Prev .Next -}}
{{ $p := where site.Pages }}
{{ with $p.Next . -}}
{{ end -}}
{{ end -}}
```
|
## GoHugo / `.html`
To use it with GoHugo and basic `.html` files, you'll have to override the used parser inside your `.prettierrc` file:
```js
{
overrides: [
{
files: ["*.html"],
options: {
parser: "go-template",
},
},
],
}
```
## VSCode
Make sure to always have installed **both** dependencies:
- `prettier`
- `prettier-plugin-go-template`
Also make sure that they are installed inside the same scope.
Install both globally (`npm i -g`) or locally – otherwise prettier may not pick up the plugin.
> Note: The global setup additional requires setting your VSCode prettier path to your global prettier path. You can read in [this issue](https://github.com/NiklasPor/prettier-plugin-go-template/issues/58#issuecomment-1085060511) how to set it up – should be doable in less than a minute if you have npm & VSCode already running.
## Additional Options
```js
// .prettierrc
{
/**
* Enables & disables spacing between go statements.
* E.g. {{ statement }} vs {{statement}}.
* Default: true
*/
"goTemplateBracketSpacing": true
}
```
## Ignoring Code
#### Single Block
```html
{{if }}
{{end }}
```
#### Multiline
```html
{{/* prettier-ignore-start */}}
{{/* prettier-ignore-end */}}
```
## Changelog
### v0.0.12
- Fix several formatting issues
- Improve unformattable script & style detection
- Huge thanks to @jasikpark for validating & cleaning up the issues 🎉
### v0.0.11
- AST rewrite
- Fix inline actions
- If / Else / Else-If support
- Ignore formatting for blocks with `{{/* prettier-ignore */}}
- Ignore large code sections with `{{/* prettier-ignore-start */}}...{{/* prettier-ignore-end */}}
- Tweak general formatting
- Support for multiline actions
### v0.0.10
- Resolve bug #19: Fix template comments.
### v0.0.9
- Resolve bug of single line if statements.
### v0.0.8
- Go block statements will now be indented accordingly. Except for `else`.
- if, range, block, with, define, end
### v0.0.7
- Fix broken shortcodes. Thanks to @alqu for discovering & fixing the bug.
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!