Is there a way in the mustache to visualize the block if the section is present without iteration?

I have a section with a heading that I want to show only if the array is full, something like this:

<h1> These are the elements in the array </h1>
<ul>
    {{#myArray}}
    <li>{{name}} - {{value}}</li>
    {{/myArray}}
</ul>

How can I <h1>only display if it is populated myArray?

If I put it in section #, it is rendered once for each element of the array, which I don't want.

What is the right way to do this?

+4
source share
1 answer
{{#myArray.length}}
     <h1> These are the elements in the array </h1>
{{/myArray.length}}

.length returns 0 for empty arrays so that we reach the real value false.

Demo

From a PHP point of view:

{{#myArray.0}}
     <h1> These are the elements in the array </h1>
{{/myArray.0}}
+3
source

Source: https://habr.com/ru/post/1541440/


All Articles