Templates
Default Variables
date
: defaults to today’s date, under universal time, printed according to ISO 8601 (for example,"2014-03-11"
)datetime
: defaults to today’s date and time, under universal time, printed according to ISO 8601 (for example,"2014-03-11T16:06:02+00:00"
).author_name
: Defaults to"Anonymous"
author_email
: Defaults to"anonymous@example.org"
apps_dir
: Directory where OTP applications should be created in release projects. Defaults to"apps/"
.copyright_year
: Defaults to the current year, under universal time.
Global Variables
Global variables can be set by editing the file at $HOME/.config/rebar3/templates/globals
:
This will let you define variables for all templates. Variables left undefined will be ignored and revert to the default value.
The override order for these variables will be: Defaults < $HOME/.config/rebar3/templates/globals < command line invocation
.
Batteries-Included Templates
Rebar3 ships with a few templates installed, which can be listed by calling rebar3 new
:
Any custom plugins would be followed as <plugin_name> (custom): <description>
.
Details for each individual plugin can be obtained by calling rebar3 new help <plugin>
:
All the variables there have their default values shown, and an optional explanation in parentheses. The variables can also be overriden globally.
A template can be run by calling:
Then go to the directory created for the project by rebar3.
Custom Templates
Custom templates can be added in $HOME/.config/rebar3/templates/
. Each template is at least two files:
my_template.erl
: There can be many of these files. They are regular Erlang files using the mustache template syntax for variable replacements (provided by soranoba’s implementation).my_template.template
; Called the template index, there is one per template callable fromrebar3
. This one will be visible when callingrebar3 new my_template
. This file regroups the different mustache template files into a more cohesive template.
File Syntax
Template Index
The following options are available:
Specifically:
description
: takes a string explaining what the template is for.variables
: takes a list of variables in two forms:{Name, DefaultString, HelpString}
;{Name, DefaultString}
.
{dir, TemplatablePathString}
: creates a given directory. Variable names can be used in the path name.{file, FilePath, DestFilePath}
: copies a file literally to its destination.{template, FilePath, TemplatablePathString}
: evaluates a given template. TheFilePath
is relative to the template index.{chmod, FilePath, Int}
: changes the permission of a file, using the integer value specified. Octal values can be entered by doing8#640
.
Example
As an example, we’ll create a template for Common Test test suites. Create the directory structure ~/.config/rebar3/templates/
and then go in there.
We’ll start with an index for our template, called ct_suite.template
:
This tells Rebar3 to create the test directory and to evaluate a mustache template. All the paths are relative to the current working directory.
Let’s create the template file:
This one does very simple variable substitution for the name (using {{name}}
) and that’s all it needs.
Let’s get to any existing project you have and try it:
The first line shows that our ct_suite
template has been detected and is usable.
Let’s look at the details:
The documentation from variables and the description are well in place. To apply the template, go to any of your OTP application’s top-level directory:
And you will see the code in place.
Plugin Templates
Plugins can be shipped with their own templates that then get listed along with the rest of templates. The structure is exactly similar to those found in ~/.config/rebar3/templates/
, except they must be placed in the plugin’s priv/
directory.