0

In Handlebars, you can make conditionals, like:

{{#if isActive}}
  <img src="star.gif" alt="Active">
{{/if}}

http://handlebarsjs.com/block_helpers.html

I've been trying for a while to do something like this in the PHP Mustache. But nowhere i can find anything allowing {{#something withvalue }}

Why not just use the {{#each }} to if something? If I want to Each over a list making lis, and I only want to wrap them in a ul when there are actually lis?

I would need an extra value that has prechecked the list, which I think is ugly.

{{#hasList }}
<ul>
{{# list }}
    <li>{{ title }}</li>
{{/ list }}
</ul>
{{/hasList }}

There must be a way to handle this in Mustache, right?

Laurens Kling
  • 2,221
  • 1
  • 21
  • 31
  • http://stackoverflow.com/a/10118092/4036946 – jsxqf Mar 14 '15 at 14:29
  • 2
    It's not ugly, it's Mustache. Each implementation has its own way to escape this feature paucity, and it looks like you can use the .count property of PHP arrays, as in {{#list.count}}
      ...
    {{/list.count}}: http://stackoverflow.com/a/19427289/525656
    – Gwendal Roué Mar 14 '15 at 17:38
  • 1
    `{{#list.count}}` only works if you have an `ArrayIterator` wrapping your array. You can fake it with a `{{#list.0}}` section tag, but in my opinion that's much uglier than adding the extra value. – bobthecow Mar 15 '15 at 00:11
  • `.count` has worked for me with an `new ArrayObject`. Thanks! – Laurens Kling Mar 16 '15 at 10:21

0 Answers0