Templates
Templates in mise provide a powerful way to configure different aspects of your environment and project settings.
A template is a string that contains variables, expressions, and control structures. When rendered, the template engine (tera
) replaces the variables with their values.
You can define and use templates in the following locations:
- Most
mise.toml
configuration values- The
mise.toml
file itself is not templated and must be valid toml
- The
.tool-versions
files- (Submit a ticket if you want to see it used elsewhere!)
Example
Here is an example of a mise.toml
file that uses templates:
[env]
PROJECT_NAME = "{{ cwd | basename }}"
[tools]
node = "{{ get_env(name='NODE_VERSION', default='20') }}"
You will find more examples in the cookbook.
Template Rendering
Mise uses tera to provide the template feature. In the template, there are 3 kinds of delimiters:
{{
and}}
for expressions{%
and%}
for statements{#
and#}
for comments
Additionally, use raw
block to skip rendering tera delimiters:
{% raw %}
Hello {{ name }}
{% endraw %}
This will become Hello {{name}}
.
Tera supports literals, including:
- booleans:
true
(orTrue
) andfalse
(orFalse
) - integers
- floats
- strings: text delimited by
""
,''
or``
- arrays: a comma-separated list of literals and/or ident surrounded by
[
and]
(trailing comma allowed)
You can render a variable by using the {{ name }}
. For complex attributes, use:
- dot
.
, e.g.{{ product.name }}
- square brackets
[]
, e.g.{{ product["name"] }}
Tera also supports powerful expressions:
- mathematical expressions
+
-
/
*
%
- comparisons
==
!=
>=
<=
<
>
- logic
and
or
not
- concatenation
~
, e.g.{{ "hello " ~ 'world' ~ `!` }
- in checking, e.g.
{{ some_var in [1, 2, 3] }}
Tera also supports control structures such as if
and for
. Read more here.
Tera Filters
You can modify variables using filters. You can filter a variable by a pipe symbol (|
) and may have named arguments in parentheses. You can also chain multiple filters. e.g. {{ "Doctor Who" | lower | replace(from="doctor", to="Dr.") }}
will output Dr. who
.
Tera Functions
Functions provide additional features to templates.
Tera Tests
You can also uses tests to examine variables.
{% if my_number is not odd %}
Even
{% endif %}
Mise Template Features
Mise provides additional variables, functions, filters and tests on top of tera features.
Variables
Mise exposes several variables. These variables offer key information about the current environment:
env: HashMap<String, String>
– Accesses current environment variables as a key-value map.cwd: PathBuf
– Points to the current working directory.config_root: PathBuf
– Locates the directory containing yourmise.toml
file, or in the case of something like~/src/myproj/.config/mise.toml
, it will point to~/src/myproj
.mise_bin: String
- Points to the path to the current mise executablemise_pid: String
- Points to the pid of the current mise processxdg_cache_home: PathBuf
- Points to the directory of XDG cache homexdg_config_home: PathBuf
- Points to the directory of XDG config homexdg_data_home: PathBuf
- Points to the directory of XDG data homexdg_state_home: PathBuf
- Points to the directory of XDG state home
Functions
Tera offers many built-in functions. []
indicates an optional function argument. Some functions:
range(end, [start], [step_by])
- Returns an array of integers created using the arguments given.end: usize
: stop beforeend
, mandatorystart: usize
: where to start from, defaults to0
step_by: usize
: with what number do we increment, defaults to1
now([timestamp], [utc])
- Returns the local datetime as string or the timestamp as integer.timestamp: bool
: whether to return the timestamp instead of the datetimeutc: bool
: whether to return the UTC datetime instead of the local one- Tip: use date filter to format date string. e.g.
{{ now() | date(format="%Y") }}
gets the current year.
throw(message)
- Throws with the message.get_random(end, [start])
- Returns a random integer in a range.end: usize
: upper end of the rangestart: usize
: defaults to 0
get_env(name, [default])
: Returns the environment variable value by name. Preferenv
variable than this function.name: String
: the name of the environment variabledefault: String
: a default value in case the environment variable is not found. Throws when can't find the environment variable anddefault
is not set.
Tera offers more functions. Read more on tera documentation.
Mise offers additional functions:
exec(command) -> String
– Runs a shell command and returns its output as a string.arch() -> String
– Retrieves the system architecture, such asx86_64
orarm64
.os() -> String
– Returns the name of the operating system, e.g. linux, macos, windows.os_family() -> String
– Returns the operating system family, e.g.unix
,windows
.num_cpus() -> usize
– Gets the number of CPUs available on the system.choice(n, alphabet)
- Generate a string ofn
with random sample with replacement ofalphabet
. For example,choice(64, HEX)
will generate a random 64-character lowercase hex string.
An example of function using exec
:
[alias.node.versions]
current = "{{ exec(command='node --version') }}"
Exec Options
The exec
function supports the following options:
command: String
– [required] The command to run.cache_key: String
– The cache key to store the result. If the cache key is provided, the result will be cached and reused for subsequent calls.cache_duration: String
– The duration to cache the result. The duration is in seconds, minutes, hours, days, or weeks. e.g.cache_duration="1d"
will cache the result for 1 day.
Filters
Tera offers many built-in filters. []
indicates an optional filter argument. Some filters:
str | lower -> String
– Converts a string to lowercase.str | upper -> String
– Converts a string to uppercase.str | capitalize -> String
– Converts a string with all its characters lowercased apart from the first char which is uppercased.str | replace(from, to) -> String
– Replaces a string with all instances offrom
toto
. e.g.,{{ name | replace(from="Robert", to="Bob")}}
str | title -> String
– Capitalizes each word inside a sentence. e.g.,{{ "foo bar" | title }}
becomesFoo Bar
.str | trim -> String
– Removes leading and trailing whitespace.str | trim_start -> String
– Removes leading whitespace.str | trim_end -> String
– Removes trailing whitespace.str | truncate -> String
– Truncates a string to the indicated length.str | first -> String
– Returns the first element in an array or string.str | last -> String
– Returns the last element in an array or string.str | join(sep) -> String
– Joins an array of strings with a separator, such as{{ ["a", "b", "c"] | join(sep=", ") }}
to producea, b, c
.str | length -> usize
– Returns the length of a string or array.str | reverse -> String
– Reverses the order of characters in a string or elements in an array.str | urlencode -> String
– Encodes a string to be safely used in URLs, converting special characters to percent-encoded values.str | map(attribute) -> Array
– Extracts an attribute from each object in an array.str | concat(with) -> Array
– Appends values to an array.str | abs -> Number
– Returns the absolute value of a number.str | filesizeformat -> String
– Converts an integer into a human-readable file size (e.g., 110 MB).str | date(format) -> String
– Converts a timestamp to a formatted date string using the provided format, such as{{ ts | date(format="%Y-%m-%d") }}
. Find a list of time format onchrono
documentation.str | split(pat) -> Array
– Splits a string by the given pattern and returns an array of substrings.str | default(value) -> String
– Returns the default value if the variable is not defined or is empty.
Tera offers more filters. Read more on tera documentation.
Hash
str | hash([len]) -> String
– Generates a SHA256 hash for the input string.len: usize
: truncates the hash string to the given size
path | hash_file([len]) -> String
– Returns the SHA256 hash of the file at the given path.len: usize
: truncates the hash string to the given size
Path Manipulation
path | canonicalize -> String
– Converts the input path into absolute input path version. Throws if path doesn't exist.path | basename -> String
– Extracts the file name from a path, e.g./foo/bar/baz.txt
becomesbaz.txt
.path | file_size -> String
– Returns the size of a file in bytes.path | dirname -> String
– Returns the directory path for a file, e.g./foo/bar/baz.txt
becomes/foo/bar
.path | basename -> String
– Returns the base name of a file, e.g./foo/bar/baz.txt
becomesbaz.txt
.path | extname -> String
– Returns the extension of a file, e.g./foo/bar/baz.txt
becomes.txt
.path | file_stem -> String
– Returns the file name without the extension, e.g./foo/bar/baz.txt
becomesbaz
.path | file_size -> String
– Returns the size of a file in bytes.path | last_modified -> String
– Returns the last modified time of a file.path[] | join_path -> String
– Joins an array of paths into a single path.
For example, you can use split()
, concat()
, and join_path
filters to construct a file path:
[env]
PROJECT_CONFIG = "{{ config_root | concat(with='bar.txt') | join_path }}"
String Manipulation
str | quote -> String
– Quotes a string. Converts'
to\'
and then quotes str, e.g'it\'s str'
.str | kebabcase -> String
– Converts a string to kebab-casestr | lowercamelcase -> String
– Converts a string to lowerCamelCasestr | uppercamelcase -> String
– Converts a string to UpperCamelCasestr | snakecase -> String
– Converts a string to snake_casestr | shoutysnakecase -> String
– Converts a string to SHOUTY_SNAKE_CASE
Tests
Tera offers many built-in tests. Some tests:
defined
- Returnstrue
if the given variable is defined.string
- Returnstrue
if the given variable is a string.number
- Returnstrue
if the given variable is a number.starting_with
- Returnstrue
if the given variable is a string and starts with the arg given.ending_with
- Returnstrue
if the given variable is a string and ends with the arg given.containing
- Returnstrue
if the given variable contains the arg given.matching
- Returnstrue
if the given variable is a string and matches the regex in the argument.
Tera offers more tests. Read more on tera documentation.
Mise offers additional tests:
if path is dir
– Checks if the provided path is a directory.if path is file
– Checks if the path points to a file.if path is exists
– Checks if the path exists.