11Level 12: use template engine extensions
22================================================================================
33
4+ There are three possible ways to provide extensions for your template engine.
5+ Let's take jinja2 as an example.
6+
7+ 1. Ready made extensions
8+ -----------------------------
9+
410jinja2 comes with a lot of extensions. In order not to be the blocker in the
511middle, **extensions ** is allowed in moban file to initialize jinja2 engine
612with desired extensions. Two extensions, expression-statement and loop-controls
@@ -21,9 +27,61 @@ For example::
2127Please also note that the following extensions are included by default:
2228`jinja2.ext.do `, `jinja2.ext.loopcontrols `
2329
24- .. note ::
2530
26- if you intend to use extensions for one off usage, please use '-e' cli option
31+ **Command line **
32+
33+ if you intend to use extensions for one off usage, please use '-e' cli option.
34+ for example: `moban -e jinja2=your_custom_jinja2_extension <https://github.com/chfw/math-sheets/blob/master/reception/a_op_b_op_c/make.sh >`_
35+
36+
37+ 2. Ad-hoc declaration
38+ -----------------------------
39+
40+ Let's say you are fond of some existing functions, for example, ansible's combine
41+ filter. With moban, you can immediately include it for your template via the following
42+ syntax:
43+
44+ .. code-block ::
45+
46+ extensions:
47+ jinja2:
48+ - filter:module.path.filter_function
49+ - test:module.path.test_function
50+ - global:identifier=module.path.variable
51+
52+ For example::
53+
54+ extensions:
55+ jinja2:
56+ - filter:ansible.plugins.filter.core.combine
57+ - test:moban.externals.file_system.exists
58+
59+ **Command line **
60+
61+ .. code-block :: bash
62+
63+ $ moban -e jinja2=filter:module.path.filter_function jinja2=test:module.path.test_function jinja2=global:identifier=module.path.variable
64+
65+ you can do this::
66+
67+ $ moban -e jinja2=filter:module.path.filter_function \
68+ jinja2=test:module.path.test_function \
69+ jinja2=global:identifier=module.path.variable
70+
71+
72+ 3. Make your own extensions
73+ --------------------------------
74+
75+ You can choose to write an extension for the template type of your choice.
76+ For example, you can write a reusable extension for jinja2. moban will be
77+ able to load it as it is.
78+
79+ If you decide that you only want to write them for moban but for your own
80+ use, you can follow `Level 7: Custom jinja filters, tests and globals ` and
81+ write your own. When you would like to make yours avaiable for all moban
82+ users, you can follow `moban-jinja2-github <https://github.com/moremoban/moban-jinja2-github >`_ and
83+ `moban-ansible <https://github.com/moremoban/moban-ansible >`_
84+
2785
2886Evaluation
2987--------------------------------------------------------------------------------
0 commit comments