Debian Codegraph @

docs/template/template-methods.md

raw
1 # Template methods
2
3 Besides [Jinja2 global functions](https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-global-functions),
4 the following methods can be used within templates:
5
6 * `exists(path)`: returns true when path exists
7 ```
8 {%@@ if exists('/dev/null') @@%}
9 it does exist
10 {%@@ endif @@%}
11 ```
12
13 * `exists_in_path(name, path=None)`: returns true when executable exists in `$PATH`
14 ```
15 {%@@ if exists_in_path('exa') @@%}
16 alias ls='exa --git --color=always'
17 {%@@ endif @@%}
18 ```
19
20 * `basename(path)`: returns the `basename` of the path argument
21 ```
22 {%@@ set dotfile_filename = basename( _dotfile_abs_dst ) @@%}
23 dotfile dst filename: {{@@ dotfile_filename @@}}
24 ```
25
26 * `dirname(path)`: returns the `dirname` of the path argument
27 ```
28 {%@@ set dotfile_dirname = dirname( _dotfile_abs_dst ) @@%}
29 dotfile dst dirname: {{@@ dotfile_dirname @@}}
30 ```
31
32 Custom user-defined functions can be loaded with the help of the
33 config entry `func_file`.
34
35 Example:
36
37 The config file:
38 ```yaml
39 config:
40 func_file:
41 - /tmp/myfuncs_file.py
42 ```
43
44 The python function under `/tmp/myfuncs_file.py`:
45 ```python
46 def myfunc(arg):
47 return not arg
48 ```
49
50 The dotfile content:
51 ```
52 {%@@ if myfunc(False) @@%}
53 this should exist
54 {%@@ endif @@%}
55 ```