1# Example showing how sections are rendered using cfengine classes
2
3# Being a logicless templating system, mustache is not able to leverage
4# CFEngine's powerful class expression logic. Only singular classes can be used
5# to conditionally render a block in mustache. This example shows how you can
6# define a singular cfengine class based on a complex expression, and then use
7# that singular class for conditional rendering in a template.
8
9#+begin_src cfengine3
10bundle agent main
11{
12  classes:
13
14      "known_day_of_week"
15        expression => "(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)";
16
17  vars:
18
19      "rendered"
20        string => string_mustache(
21"{{#classes.known_day_of_week}}I recognize the day of the week.{{/classes.known_day_of_week}}
22{{^classes.class_you_are_looking_for}}
23The class you are looking for is not defined.
24{{/classes.class_you_are_looking_for}}",
25                                  datastate());
26  reports:
27      "$(rendered)";
28
29}
30#+end_src
31
32#+begin_src policy_description
33#@ Here we define the class `known_day_of_week` as long as there is a class
34#@ representing a known day. Then we render the value of the string variable
35#@ "rendered" using `string_mustache()` with a template that includes a section
36#@ that is conditional when `classes.known_day_of_week` is `true` and another section
37#@ when `classes.class_you_are_looking_for` is not defined based on the data
38#@ provided from `datastate()` which is the default set of data to use for mustache
39#@ templates when explicit data is not provided. Finally we report the variable to
40#@ see the rendered template.
41#+end_src
42#+begin_src example_output
43#@ ```
44#@ R: I recognize the day of the week.
45#@ The class you are looking for is not defined.
46#@
47#@ ```
48#+end_src
49#+begin_src output_description
50#@ We can see in the output that the conditional text was rendered as expected.
51#@ Try adjusting the template or the class expression.
52#+end_src
53