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

Ability to fail mustache template rendering if a variable is missed




      The mustache manual says "By default a variable "miss" returns an empty string. This can usually be configured in your Mustache library. The Ruby version of Mustache supports raising an exception in this situation, for instance."

      This behavior is observed in the online demo. To observe it simply remove the line '"header": "Colors",' from the sample JSON data and see in the rendered template, that there is an empty string between the h1 tags in the first line.

      It could be especially useful to have the ability to configure the behavior of a variable miss within the agent, as for system configuration files, if you do not have some data you are looking for it may be very bad to render without it.


      Proposal 1

      I think there should be an option in body agent control (since only agents render templates) to change this default behavior.

      body agent control
        mustache_fail_missed_variable => "false"; # (false|true)

      I also think that there should be an option to alter this behavior at the individual promise level. Perhaps a mustache_fail_missed_variable attribute?

      bundle agent example
            edit_template => "/tmp/myfile.mustache",
            template_method =>"mustache",
            mustache_fail_missed_variable => "true";

      Question: How would/should string_mustache() be affected by this?

      Proposal 2

      Make it an extension to the template language using bash syntax:

      • missing var, template {{ var:- }} = empty string
      • missing var, template {{var}} = empty string (backwards compatibility)
      • missing var, template {{ var:-foo }} = foo
      • present var, template {{ var:-foo }} = value of var

      That's more targeted and doesn't change the current behavior, whatever that is.


          Issue Links



              • Assignee:
                a10042 Nick Anderson
              • Votes:
                1 Vote for this issue
                4 Start watching this issue


                • Created:

                  Summary Panel