I am working on a project in Kotlin that generates XML files using templates with Handlebars (in the server side , the version 4.0.6). I have the following class in a .kt file:
class miClase(val codigo: String, val nombre: String?)
And this in a template .hbs:
...
{{#each misClases}}
<elemento code="{{codigo}}" name="{{nombre}}">
{{/each}}
...
And it works without problems. The XML is generated with a list of elements. Now I have been asked that the part of the template that is generated from a list of miClase
should not include empty elements. That is, if the code or name is missing, it will be displayed, but if both are missing, they will not be displayed.
For that I have changed the class to this:
class miClase(val codigo: String, val nombre: String?) {
var isVisible = true
init {
if (codigo.isNullOrEmpty() && nombre.isNullOrEmpty()) {
isVisible = false
}
}
}
And I have changed the template to make it like this:
...
{{#each misClases}}
{{#if isVisible}}
<elemento code="{{codigo}}" name="{{nombre}}">
{{/if}}
{{/each}}
...
Everything seems correct, but the data is not displayed. I have done debug, and I can see that the list of objects is there and that everyone has the property isVisible
with the value true
so that part of the template should be filled and visible, but it is not.
What happens is not that the values appear empty, but that the elemento
is not even loaded directly.
Why does this happen? What is wrong with the code?