Вопрос по – handlerbars.js проверить, если список пуст

108

Есть ли способ в шаблоне Handlebars.js проверить, является ли коллекция или список нулевым или пустым, перед тем, как перейти к списку / коллекции?

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

{{/list}}



{{#each items}}

{{/each}}

Ваш Ответ

5   ответов
34

{{#if items}}
// render items

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

{{else}}
// render empty
{{/if}}
Error: User Rate Limit ExceededifError: User Rate Limit ExceededHandlebars documentationError: User Rate Limit Exceededfalse, undefined, null, "", 0Error: User Rate Limit Exceeded[]Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Drejc
Error: User Rate Limit ExceededitemsError: User Rate Limit Exceeded[]Error: User Rate Limit Exceededitems.lengthError: User Rate Limit Exceeded@Duane's answer.
190

{{#each items}}
// render item
{{else}}
// render empty
{{/each}}
Error: User Rate Limit Exceeded
210

Error: User Rate Limit ExceededonceError: User Rate Limit Exceededonly if the array has dataError: User Rate Limit Exceeded

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

.lengthError: User Rate Limit Exceeded

Error: User Rate Limit Exceededhandlebars docsError: User Rate Limit Exceeded[]Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded<ul>Error: User Rate Limit Exceeded<li>Error: User Rate Limit Exceeded<ul>Error: User Rate Limit Exceeded<p>empty list<p>Error: User Rate Limit Exceeded<ul>Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Drejc
1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

{{#each OtherRandomItems}}

  {{this}}

  {{lookup ../AnotherRandomItems @index}}

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

{{/each}}
7

Error: User Rate Limit Exceededcount()Error: User Rate Limit Exceeded

{{#if items.count}}
    <p>There is {{items.count}} item(s).</p>
{{else}}
    <p>There is nothing</p>
{{/if}}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы