Dotdrop Templates

· Recent Quackings

Implementing templates for dotdrop.

Implementing dotfile templates in jinja for dotdrop. #

Only the developer, and maybe perhaps God, knows why dotdrop uses a custom version of the Jinja templating system, but here we are. Isn't it bloody glorious?

You would think there would have been a better option?

Chezmoi is looking real good right about now.

Creating your new abomination of markup #

So, let's get down to business and run this railroad right into the ground.

Delimiters #

Well, as you know, delimiters delimit things. Which means they indicate a beginning and an end to a region or section of text that needs distinction from the text around it. The delimiters used by dotdrop are what makes it distinct from standard jinja templating markup. Here they are.

The Header #

Although, not required, it is reccommended to begin your dotdrop template with a bloody header, like so:

1{{@@ header() @@}}

Variables #

[!info] I Shamelessly copied this directly from the dotdrop docs to save time.

Template variables #

Available variables #

The following variables are available in templates:

Enriched variables #

The below variables are added to the available variables within templates. If the variable is already set by the user (through the config file for example) it will not be overwritten.

Dotfile variables #

When a dotfile is handled by dotdrop, the following variables are also available for templating:

In addition to the above, the following variables are set in each file processed by dotdrop:

For example, a directory dotfile (like ~/.ssh) would process several files (~/.ssh/config and ~/.ssh/authorized_keys, for example). In ~/.ssh/config:

Environment variables #

It's possible to access environment variables inside the templates:

{{@@ env['MY_VAR'] @@}}

This allows for storing host-specific properties and/or secrets in environment variables. It is recommended to use variables (see config variables) instead of environment variables unless these contain sensitive information that shouldn't be versioned in Git (see handle secrets doc).

Variables dictionary #

All variables are also available through the dictionary _vars

{%@@ for key in _vars @@%}
key:{{@@ key @@}} - value:{{@@ _vars[key] @@}}
{%@@ endfor @@%}

Methods #

[!info] I did the same thing here.

Besides Jinja2 global functions, the following methods can be used within templates:

{%@@ if exists('/dev/null') @@%}
it does exist
{%@@ endif @@%}
{%@@ if exists_in_path('exa') @@%}
alias ls='exa --git --color=always'
{%@@ endif @@%}
{%@@ set dotfile_filename = basename( _dotfile_abs_dst ) @@%}
dotfile dst filename: {{@@ dotfile_filename @@}}
{%@@ set dotfile_dirname = dirname( _dotfile_abs_dst ) @@%}
dotfile dst dirname: {{@@ dotfile_dirname @@}}

Custom user-defined functions can be loaded with the help of the config entry func_file.

Example:

The config file:

1config:
2  func_file:
3  - /tmp/myfuncs_file.py

The python function under /tmp/myfuncs_file.py:

1def myfunc(arg):
2  return not arg

The dotfile content:

{%@@ if myfunc(False) @@%}
this should exist
{%@@ endif @@%}

Filters #

[!info] And I could have been different and written this out, but I didn't.

Besides Jinja2 builtin filters, custom user-defined filter functions can be loaded using the config entry filter_file:

Example:

The config file:

1config:
2  filter_file:
3  - /tmp/myfilter_file.py

The python filter under /tmp/myfilter_file.py:

1def myfilter(arg1):
2  return str(int(arg1) - 10)

The dotfile content:

{{@@ "13" | myfilter() @@}}

For more information on how to create filters, see the Jinja2 official docs.

last updated:

Home