I'm using JMustache, but I imagine this question would be the same for all implementations.
I'm using Mustache to Generate an XML file. When a list is empty, I don't want the parent tag to show. When the list is not empty, I want the parent tag to show once. I'm wondering what the Mustache Template should look like.
For example I might have either of the two XML files that need to be generated based on the data input:
<class>
    <name>Basketweaving</name>
    <students>
        <student>Joe Smith</student> 
        <student>Sally Smithers</student>
    </students>
</class>
or:
<class>
    <name>Basketweaving at a bad time</name>
</class>
The problem I'm having is if I define my template like this:
<class>
   <name>{{className}}</name>
   <students>
    {{#students}}
      <student>{{studentName}}</student>
    {{/students}}
   </students>
<class>
Then the empty class still has a students block.
e.g.
  <class>
        <name>Basketweaving at a bad time</name>
        <students>
</students>
    </class>
And if I move the loop:
<class>
   <name>{{className}}</name>
   {{#students}}
   <students>
      <student>{{studentName}}</student>
   </students>
   {{/students}}
<class>
I'll end up with Students repeated in the first example:
e.g.
<class>
    <name>Basketweaving</name>
    <students>
        <student>Joe Smith</student> 
    </students>
    <students>
        <student>Sally Smithers</student>
    </students>
</class>
So, what is the proper way to do the template to get my desired behavior?
 
     
    