How do I identify the current page in a page collection?

核能气质少年 提交于 2019-12-11 02:05:25

问题


I'm configuring a static site generator using Grunt's Assemble.io. I'm using the Pages Collection to build a navigation list like so:

{{#each pages }}
    <li><a href="{{{filename}} }">{{{title}} }</a></li>
{{/each }}

I would also like to apply class="active" to the navigation item for the current page, but I'm not sure how to approach this. My first thought was to try comparing the title of the current iteration of the collection to that of the page being rendered, but this has two problems:

  1. The if helper in handlebars doesn't seem to have a way to compare two expressions. I could write a custom helper, but I would want to know this is the best way before I try it.
  2. I don't know how to distinguish between the title of the current collection iteration and that of the current page. They would both be referred to as simply title as far as I can tell.

How can I determine when the pages collection loop is operating on the page which is the current page being rendered?


回答1:


Use isCurrentPage

{{#each pages }}
<li{{#if this.isCurrentPage}} class="active"{{/if}}>
    <a href="{{{filename}} }">{{{title}} }</a>
</li>
{{/each }}

Alternatively, you can use isActive helper.



来源:https://stackoverflow.com/questions/21630251/how-do-i-identify-the-current-page-in-a-page-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!