javascriptmustachemustache.php

how to limit iterations in mustache


I have this mustache template:

{{#pages}}
  <ul class="nav navbar-nav">
    <li><a href="page/{{slug}}.html">{{title}}</a></li>
  </ul>
{{/pages}}

Now there are more than 50 pages but I want to be able to show only 10 pages. How to do that in mustache eg limiting the iterations to specific number something like:

{{#pages:10}} <-- 10 added here as example
  <ul class="nav navbar-nav">
    <li><a href="page/{{slug}}.html">{{title}}</a></li>
  </ul>
{{/pages}}

I searched the documentation but could not find the solution.

Thanks for the help


Solution

  • Handlebars is designed as a logic-less templating system. They don't want you to do this in the template.

    That said, you should restructure your data (in js/php/whatever) before it gets to the template. e.g...

    var firstPage = pages.slice(0, 10);