Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3128

Mustache templates should allow iteration over the values in an object without referring to the keys



    • Type: Bug
    • Status: Open
    • Priority: (None)
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: cf-agent
    • Labels:


      The handling of iteration over objects vs. lists in mustache templates is a little bit inconsistent/unpredictable (or at least cannot be predicted from the documentation).

      Here is a test that doesn't work at all:

      bundle agent main {
              create => "true",
              template_method => "inline_mustache",
              edit_template_string => "
      Should have three items: {{{.}}}
      This line should appear three times
      Three words for numbers: {{{@}}} means {{{.}}}
      Should show three words: {{{.}}}
      Should show three numerals: {{{@}}}
      This line should be here three times also
              template_data => @(mydata);
              data => '{
                "list_no_ref_to_value": [
                "list": [
                "obj": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                "obj_bad_refs": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                "obj_bad_refs2": {
                  "1": "one",
                  "2": "two",
                  "3": "three",
                "obj_bad_refs3": {
                  "1": "one",
                  "2": "two",
                  "3": "three",

      This produces the error:

         error: Error rendering mustache template '(null)'

      Whatever that means.

      Removing these lines allows the template to at least render (although they shouldn't need to be removed):

      -Should show three words: {{{.}}}

      Then it produces these results:

      Should have three items: 1
      Should have three items: 2
      Should have three items: 3
      This line should appear three times
      This line should appear three times
      This line should appear three times
      Three words for numbers: 1 means one
      Three words for numbers: 2 means two
      Three words for numbers: 3 means three
      Should show three numerals: 1
      Should show three numerals: 2
      Should show three numerals: 3
      This line should be here three times also


      1. A data container (object) cannot be iterated over solely by value, whereas a list can be. This is a bug. (To include the values from an object in the rendered file, you MUST include the keys.) This is the test that throws the error above and doesn't render at all.
      2. A data container that is iterated over without any reference to the keys, will produce only ONE line in the rendered output. Contrast this with the handling for a list, where iterating over a list without referring to the values (using the "." variable) will still produce as many lines (blocks) as there are elements in the list. This is a bug.

      I haven't tested the handling for CFEngine slists and classic arrays that are passed by top-level key, but I anticipate that these same bugs will hold true. (Also see CFE-3124 for an additional bug about CFEngine classic arrays.)




            • Assignee:
              mweilgart Mike Weilgart
            • Votes:
              0 Vote for this issue
              1 Start watching this issue


              • Created:

                Summary Panel