Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withSort not working for pages collection #50

Closed
toby1kenobi opened this issue Oct 1, 2013 · 11 comments
Closed

withSort not working for pages collection #50

toby1kenobi opened this issue Oct 1, 2013 · 11 comments

Comments

@toby1kenobi
Copy link

The final section here talks about sorting the pages collection:

<ul>
    {{#withSort pages data.date}}
        <li>{{data.title}}</li>
    {{/withSort}}
</ul>

If I add that code to a page within the boilerplate-bootstrap project, for example, the build fails with

Warning: Cannot read property 'hash' of undefined Use --force to continue
Aborted due to warnings.

This will build

    {{#withSort pages date}}

But the output is not sorted.

Am I missing something, should this work?

@doowb
Copy link
Member

doowb commented Oct 1, 2013

Try putting quotes around the field...

<ul>
    {{#withSort pages "data.date"}}
        <li>{{data.title}}</li>
    {{/withSort}}
</ul>

If this works, the docs should be updated.

@toby1kenobi
Copy link
Author

That seems to work, thanks for that, much appreciated!

@jonschlinkert
Copy link
Member

created a new issue to update docs, since this is resolve I'll go ahead and close it

@dvera123
Copy link

This works fine, but just sort by string, if you compare [1,2,10] output will be 1, 10, 2,

If you need to make an index, try something like a-001, a-002.

@jonschlinkert
Copy link
Member

@dvera123 thanks, good to know.

@jonschlinkert jonschlinkert reopened this Feb 19, 2014
@mattdwen
Copy link

Is there any way to do a desc sort on pages, without having to explicitly declare the collection in the Gruntfile?

E.g. It would be great if you could declare {{#withSort pages "data.date" desc}}

@jonschlinkert
Copy link
Member

closing, we can continue discussion on handlebars-helpers repo

@ghost
Copy link

ghost commented Sep 17, 2014

I tried these options to no avail..

{{#withSort pages "data.sortOrder"}}
                {{#is data.section "main"}}
                    <li{{#is ../../page.dest this.dest}} class="active"{{/is}}>
                                      <a href="{{relative ../../page.dest this.dest}}">{{data.menutitle}}</a>
                                  </li>
                {{/is}}
            {{/withSort}}
layout: home.hbs
title: What is ZMIX?
section: main
sortOrder: a-000
---

Help?

@webercoder
Copy link

@mattdwen @MayhemChaos I know it's been a while, but I suppose for people in the future who have arrived here like me...

Handlebars helpers can take extra arguments (specified by name) after the default ones are provided. For more info on that, search for options.hash on this page: http://handlebarsjs.com/expressions.html

The withSort helper that the assemble.io guys wrote does this as well. If you provide dir="desc" after your arguments, the loop's array will be reversed and you'll be good to go!

{{#withSort pages "data.created" dir="desc" }}
  {{#unless data.exclude}}
    <li><a href="{{data.url}}">{{data.title}}</a></li>
  {{/unless}}
{{/withSort}}

Here is the code that does it:
https://github.com/assemble/handlebars-helpers/blob/master/lib/helpers/helpers-collections.js#L279

@pacav69
Copy link

pacav69 commented Sep 19, 2015

Refer to my 'Tip: Using the #withSort option for creating navigation menu

helpers/handlebars-helpers#200

@satuweb
Copy link

satuweb commented Apr 13, 2016

@webercoder Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants