Vincent Garrigues     Blog     Talks     Feed

Making the most of AppCode's file templates.

AppCode’s File and Code templates are generated using Velocity, a Java-based template engine. Taking a few minutes to tweak the existing templates orr create new ones can end up saving you a ton of time down the line.

What’s a template engine?

A template processor (also known as a template engine or template parser) is software designed to combine one or more templates with a data model to produce one or more result documents. (Template processor)

You can find the existing templates in Preferences > Editor > File and Code templates. The data model is a set of predefined template variables:

  • ${PROJECT_NAME} - the name of the current project.
  • ${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
  • ${USER} - the login name of the current user.
  • ${DATE} - the current system date.
  • ${TIME} - the current system time.
  • ${YEAR} - the current year.
  • ${MONTH} - the current month.
  • ${DAY} - the current day of the month.
  • ${HOUR} - the current hour.
  • ${MINUTE} - the current minute.
  • ${PRODUCT_NAME} - the name of the IDE in which the file will be created.
  • ${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
  • ${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.

You can also add your own variables. They’ll be added to the file creation prompt:

New file prompt

Combining the template with the data model is actually very easy. A good example I came across is the creation of test files. Let’s say you want to write tests for MyClass, you need to create a MyClassSpec.h file. A good starting point for that file could be the following:

The problem is that it’s not possible to generate this file using only the predefined template variables, because ${NAME} will equal MyClassSpecs and not MyClass. You can either add another variable or use Velocity to get the desired result. We’ll look into the latter.

It’s possible to execute Java code in velocity templates, which means that we can use Java’s String methods to manipulate the file name and extract the base class’ name:

In the end, this is what the Spec template looks like:

That’s it!

I’ve setup a quick project to experiment with [Velocity] templates, you’ll find it here: https://github.com/garriguv/velocity