IT박스

handlerbars.js 목록이 비어 있는지 확인

itboxs 2020. 7. 24. 07:54
반응형

handlerbars.js 목록이 비어 있는지 확인


Handlebars.js에서 컬렉션이나 목록이 null인지 또는 비어 있는지 확인하는 방법이 있습니까? 목록 / 컬렉션을 반복하기 전에?

// if list is empty do some rendering ... otherwise do the normal
{{#list items}}

{{/list}}



{{#each items}}

{{/each}}

"each"태그도 "else"섹션을 사용할 수 있습니다. 가장 간단한 형태는 다음과 같습니다.

{{#each items}}
// render item
{{else}}
// render empty
{{/each}}

당신은 당신이 표시 할 것을 무언가가있는 경우 한 번배열에 데이터가있는 경우에만 사용을

{{#if items.length}}
    //Render
{{/if}}

.length 빈 배열에 대해 0을 반환하므로 실제 거짓 값을 얻습니다.


좋아, 생각보다 간단하다.

{{#if items}}
// render items

{{#each items}}
// render item
{{/each}}

{{else}}
// render empty
{{/if}}

컬렉션 (커서)이 비어 있는지 확인하려면 이전 답변이 유용하지 않으며 대신 count()메서드 를 사용해야합니다 .

{{#if items.count}}
    <p>There is {{items.count}} item(s).</p>
{{else}}
    <p>There is nothing</p>
{{/if}}

{{#if}} 위에 {{#each}}를 사용해야하는 사람 (예 : for 루프 내부의 if 루프) 그들은 세 가지 다른 배열 목록을 가지고 있습니까?

if 문 내에서 조회를 사용하면 문제가 해결됩니다. 마찬가지로 위의 답변으로 문제가 해결되지 않았습니다.

여기 내 코드가 있습니다.

{{#each OtherRandomItems}}

  {{this}}

  {{lookup ../AnotherRandomItems @index}}

  {{#if (lookup ../RandomItems @index)}}
  // render items
  {{else}}
  // render empty
  {{/if}}

{{/each}}

참고 URL : https://stackoverflow.com/questions/10381827/handlerbars-js-check-if-list-is-empty

반응형