40

I'm thinking about creating syntax highlighter for it, but I don't know the conventional abbreviation for this specific type of template.

2
  • 6
    There isn't one. You can name the file whatever you want. People may often use .tmpl, or even .html, but there's nothing enforcing that, and I often don't use extensions at all. Commented Mar 7, 2014 at 15:27
  • I think there are already some syntax highlighters for vim. maybe you can use those as a base for the template filename. Commented Mar 7, 2014 at 17:05

9 Answers 9

34

In one of the examples from the text/template godoc, they refer to ".tmpl" files.

Sign up to request clarification or add additional context in comments.

2 Comments

vim-go also uses *.tmpl for Go HTML templates: github.com/fatih/vim-go/blob/master/ftdetect/gofiletype.vim#L29
For people here looking for HTML templates, using *.html.tmpl has worked well for me in vim/neovim.
21

If you're using the Atom editor with the go-plus plugin, it provides nice syntax highlighting if you use the .gohtml extension.

3 Comments

This extension has the advantage to be clear and not ambiguous. Personally I use ".gohtml" for html templates and ".gotxt" for text templates.
I use Goland / Jetbrains IDE, this template enables the correct formatting and linters.
I prefer to avoid this option, as it does not allow for the consistency across filetypes that you get from .go.tmpl, .json.tmpl, .html.tmpl, .yaml.tmpl and so on. Your templating code ideally should not have to care what the intended filetype of the template is
21

.ext.tmpl is probably the least bad option based on my analysis of stats from one large codebase that includes quite a bit of Go code.

Proportion of files with one of the extensions (.tpl, .tmpl, .html.tmpl, .html.tpl, .thtml) that contain the pattern /[{][{](-\ )?end/.

.thtml    0%
.gohtml < 1%
.tpl     14%
.tmpl    85%

This misses simpler go templates that do not use loops or conditionals, but not in a way that should skew results.

About a third of the .tmpl files have the form .ext.tmpl of which .html.tmpl is the single most common. A visual inspection of files sampled from within this show that the extension is an accurate description of the output of the file.

Neither the suffix .tpl nor .tmpl is used exclusively for Go templates within this codebase which has a lot of code that predates the Go language. I haven't investigated carefully, but I think it's safe to say that for each of (.tpl, .tmpl), the majority of files with that extension are probably not Go templates.

3 Comments

That's interesting statistics. Could you specify which code base you analyzed, if it is open source/available publicly?
This seems rather anectodal. Why should we care what this one random project decided to do?
@TaylorO'Connor, Noone is saying that you should care; my answer attempts to address the original question which is about existing conventions. And to be clear, this is neither an anecdote nor from one project. These stats came from a large codebase that includes many separately developed and maintained projects.
18

In VSCode syntax highlighting of templates could be activated for files with extensions:

  • .gohtml or .go.html
  • .gotmpl or .go.tmpl

https://github.com/microsoft/vscode-go/issues/464

Comments

9

I'm using .html.tmpl. That makes it clear that it's a Go template which will be rendered to HTML. Furthermore, it follows the convention set by other file types like .tar.gz.

Comments

3

The html/template package uses .tmpl in their example.
My vote goes to .html.tmpl

Comments

1

In jetbrains/goland, you can download the plugin: Go Template

which use .gohtml as default.

Comments

0

For Neovim:

Using lsp-config, we can trigger gopls on files with custom extensions.
gopls (Go's language server) seems to do a good job with Go's template syntax.

To match VSCode, we can use .gohtml, gotmpl, .go.html and .go.tmpl.

"neovim/nvim-lspconfig"
config = function()
  local servers = {
    gopls = {
      filetypes = { "go", "gomod", "gowork", "gohtml", "gotmpl", "go.html", "go.tmpl" },
    },
  },
end

Comments

-1

Since you're writing the highlighter just go with what you normally use. I'm sure the community will give you feedback and eventually some sort of agreement.

As for myself, I'm using .thtml for template html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.