Formatting Golang HTML Templates with Prettier
In the previous article I discussed my setup for hot reloading a Go server using the Air package. Another quality of life improvement I made was to use Prettier to format my templates.
I was actually rather surprised to find that Go templates (I'm using the standard library html/template
package) had no default formatting tool, because Golang itself takes standardized and automatic formatting very seriously. But setting up Prettier is not too difficult. The following worked for me. First, install Prettier via NPM, as well as the appropriate plugin.
npm install prettier prettier-plugin-go-template --save-dev
Then create a .prettierrc
file with the following contents:
{
"plugins": ["prettier-plugin-go-template"]
}
And since I was using the extension .tmpl
for my HTML templates, I needed to set up an association in my VSCode settings:
"files.associations": {
"*.tmpl": "html"
},
Now, if you have Prettier set up to format your HTML files, it will no format your projects templates.