Question

I want to make a navigation sorted by weight on my Jekyll site. I'm using this plugin, but I want to show in navigation only pages with weight, instead of showing pages without weight at the end of the list.

So I changed the plugin like this:

module Jekyll
  class WeightedPagesGenerator < Generator
    safe true
    def generate(site)
      site.pages.each do |page|
        if page.data["weight"] != nil

          site.config['weighted_pages'] = site.pages.sort_by { |a| 
            a.data['weight'] }

        end
      end
    end
  end
end

But I get an error: Generating... /_plugins/sorted_navigation.rb:10:in `sort_by': comparison of NilClass with 2 failed (ArgumentError).

Any idea how to make this work?

Était-ce utile?

La solution

Since Jekyll 2.2.0 you can sort an array of objects by any object property. Sorting by weight is now possible and far more efficient than older solutions (see https://stackoverflow.com/a/25513956/1548376)

Autres conseils

I ended up not using this plugin. Instead I use liquid tags from this answer and now my navigation looks like this:

<nav>
  <ul>
    {% for weight in (1..5) %}
      {% unless p.weight %}
        {% for p in site.pages %}
          {% if p.weight == weight %}
            <li><a {% if p.url == page.url %}class="active"{% endif %} href="{{ p.url }}" title="{{ p.title }}">{{ p.title }}</a></li>
          {% endif %}
        {% endfor %}
      {% endunless %}
    {% endfor %}
  </ul>
</nav>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top