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

Add ability to iterate over top level keyless arrays in mustache templates

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Done
    • Priority: Low
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.9.0
    • Component/s: Promise type: files
    • Labels:
      None

      Description

      Take the following example,

      <pre>
      bundle agent mustache_test
      {
      vars:
      "my_object" data => parsejson('
      [

      { "name": "fred", "age": "30", }

      ,

      { "name": "george", "age": "27", }

      ]
      ');

      files:
      "/tmp/test"
      create => "true",
      edit_template => "$(def.dir_masterfiles)/test.mustache",
      perms => mog("0644", "root", "root"),
      template_method => "mustache",
      template_data => mergedata(my_object);
      }
      </pre>

      the json object looks like this,
      <pre>
      my_object[0][name] = fred
      my_object[0][age] = 30
      my_object[1][name] = george
      my_object[1][age] = 27
      </pre>

      There is no key for the top level array. You could iterate through the array using the following syntax,

      test.mustache:
      <pre>
      #.
      name age
      /.
      </pre>

        Attachments

        1. main.cf
          0.6 kB
        2. test
          0.0 kB
        3. test.mustache
          0.1 kB

          Activity

            People

            • Assignee:
              a10003 Eystein Maloy Stenberg
              Reporter:
              ccoager Cory Coager
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Summary Panel