Templating[Examples]

Background

As is always the case, there are quite a few applications that share a same set of properties. For example, we want to have uniform looks for Qt and GTK applications. Templating utility is developed under the DRY (Don't Repeat Yourself) principle, it allows to manage these shared properties in one place: change once, apply everywhere.

Syntax

Configuring

To manage shared properties, add a section [context] to dt-cli's config file. For example, to set a property named cursor-size for the gui group to value 24:

# ~/.config/dt/cli.toml
...
[context]
gui.cursor-size = 24
## Or, as TOML allows it:
#[context.gui]
#cursor-size = 24
...

See the configuration guide for detailed usages.

Applying

dt-cli uses Rust's Handlebars crate to render templates. Handlebars is tested and widely used, according to its descriptions:

INFO

Handlebars-rust is the template engine that renders the official Rust website rust-lang.org.

For example, to apply a property named cursor-size to all source files under the gui group:

...
gtk-cursor-theme-size={{{ gui.cursor-size }}}
...

With context.gui.cursor-size being set to 24 (as in previous section), the above template (in a group with name gui) will be rendered as:

# ~/.config/gtk-3.0/settings.ini
...
gtk-cursor-theme-size=24
...

INFO

The time consumed while rendering can be quite noticeable if the template being rendered is huge. To skip rendering for a group, use the renderable = false option in the config file.

dt-cli also supports basic control flow syntaxes like looping and conditioning, and other helper directives that boosts the productiveness of the templating system. Check out the hands-on guide if interested!