tl;dr .tt files aren't a special syntax; they're ERB (Embedded Ruby) files. You might see unusual syntax if they're ERB files which generate other ERB files.
If you're working with Ruby, particularly Rails and its generators, you're likely to come across files with a .tt extension. But what actually are .tt files?
Rails generators use the Thor toolkit1 which helps with building CLIs and manipulating files. So you could think of "tt" as standing for "Thor template".
If that's the case then you might reasonably ask, "What's Thor template syntax?" It took me a long time to work out that this is the wrong question. We call a tt file a Thor template file not because it's a template using Thor syntax (there's no such thing) but because it's a template used in Thor commands.
I'll say that again: A "Thor template" means "a template used in a Thor command", not "a template using ‘Thor syntax'".
The syntax which .tt files use is actually Embedded Ruby (aka ERB, or eRuby).
One thing which led me to mistakenly believe that Thor syntax was its own language is the <%% and %%> tags I sometimes see in .tt files. If this is just ERB, what are those?
To answer that, remember that in Rails generators you'll find .tt files that are used to generate ERB files. As the Rails generator docs briefly mention,
Note that [this example template] is an ERB template that renders another ERB template. So any
<%that should appear in the resulting template must be escaped as<%%in the generator template.
(Some .tt files generate Ruby code, like controllers, so they wouldn't need to use this syntax.)
<%% is actually ERB syntax, although there isn't really canonical documentation for ERB syntax, and docs on this particular tag are hard to find. I couldn't find anything about it in the current rdoc for the ERB class2, but in this old version, we find:
<%% or %%> -- replace with <% or %> respectively
To conclude, then, .tt files are used by the Thor templating engine, but their syntax is ERB. You might find some odd-looking <%% tags, but that is also ERB syntax, albeit poorly-documented, and it converts to <% in the resulting output. This allows you to create ERB templates which themselves produce ERB!
Note: You might also find files with the .tt extension if you're working with Visual Studio text templates and their T4 text template language. That looks a bit like ERB, but uses tags like <# instead. It's nothing to do with Ruby!
-
See also http://whatisthor.com ↩
-
Rails actually uses https://github.com/jeremyevans/erubi for parsing embedded Ruby rather than the ERB class from the standard library. For an overview of embedded Ruby tools and libraries and their associated documentation, check out this Stack Overflow answer. ↩
Top comments (0)