使用 Liquid 按字母顺序对帖子进行排序

发布于 2024-12-28 15:17:38 字数 267 浏览 3 评论 0 原文

有没有办法使用 Jekyll 按字母顺序对多个帖子进行排序?

我现在有这样的东西:

{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

它有效,但帖子很混乱。我想,如果按字母顺序排序,看起来会好得多。

谢谢

Is there a way to sort a number of posts alphabetically, using Jekyll?

I have something like this now:

{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

It works, but the posts are jumbled up. Would look much nicer if they were sorted alphabetically I think.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

呆橘 2025-01-04 15:17:39

我在本地站点测试了 Christian 的出色解决方案:在第一行之前输出中有一个空链接(我不知道为什么),因此第一个链接不起作用,所以我修改了他的代码插入 {% if postitems[1] %}{{ postitems[0] 行之前}}
和之后建议的 {% endif %}。 -display-posts-by-category">tanky woo 的评论。

i tested Christian's great solution in my local site: before the first row there is an empty link (i don't no why) in output, therefore the first link doesn't work, so i modified his code inserting {% if postitems[1] %} before the line <a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>and an {% endif %} after. suggested tanky woo's comment.

雨落□心尘 2025-01-04 15:17:39

如果没有插件或自定义功能,这是无法完成的。尽管如此,我们仍在努力在下一个版本中实现这一点:https://github.com /Shopify/liquid/pull/101 然后它看起来像:

{% for tag in site.tags order:ascending %} 
   ...
{% endfor %}

另请参阅:使用 Jekyll / Liquid 模板订购阵列

It cannot be done without a plugin or custom function. Although, there is an ongoing effort to implement this in the next releases: https://github.com/Shopify/liquid/pull/101 and then it would look like:

{% for tag in site.tags order:ascending %} 
   ...
{% endfor %}

See also: Order an array with Jekyll / liquid template

£噩梦荏苒 2025-01-04 15:17:38

可以无需插件即可完成,这意味着它可以与Github Pages配合使用。

不过,您必须使用一些丑陋的字符串操作技巧。
我使用了类似的方法来实现标签页面(列出每个标签的所有帖子)

相同的方法,稍作修改:

{% capture posts %}
  {% for post in site.posts %}
    |{{ post.title }}#{{ post.url }}
  {% endfor %}
{% endcapture %}
{% assign sortedposts = posts | split: '|' | sort %}
{% for post in sortedposts %}
    {% assign postitems = post | split: '#' %}
    <a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>
{% endfor %}

注意:

第一个循环内需要两个不同的分隔符(当然,在稍后的 split 调用中也需要).
为了使其发挥作用,这两个字符都不能出现在任何帖子标题或 URL 中!

我使用的是 |#在这个例子中,它对我有用(我刚刚用我的博客测试了它)。但您可能需要使用不同的字符,具体取决于您的帖子标题以及 URL 的构建方式。


奖励:

如果您只想显示某个标签/类别中的帖子(而不是所有帖子),您可以更改第一个 for 循环(即在 capture 中) 到以下之一:

{% for post in site.tags['whatever'] %}

{% for post in site.categories['whatever'] %}

It can be done without a plugin, which means that it works with Github Pages.

You have to use some ugly string manipulation tricks, though.
I used a similar approach to implement a tag page (that lists all posts for each tag).

Same approach, slightly modified:

{% capture posts %}
  {% for post in site.posts %}
    |{{ post.title }}#{{ post.url }}
  {% endfor %}
{% endcapture %}
{% assign sortedposts = posts | split: '|' | sort %}
{% for post in sortedposts %}
    {% assign postitems = post | split: '#' %}
    <a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>
{% endfor %}

Beware:

You need two different separator characters inside the first loop (and of course again in the split calls later on).
In order for this to work, both characters must not occur in any of the post titles or URLs!!

I'm using | and # in this example, which works for me (I just tested it with my blog). But you might need to use different characters, depending on your post titles and how your URLs are constructed.


Bonus:

If you want to display only the posts in a certain tag/category (and not all posts), you can change the first for loop (the one inside the capture) to one of these:

{% for post in site.tags['whatever'] %}

{% for post in site.categories['whatever'] %}
染年凉城似染瑾 2025-01-04 15:17:38

根据文档,要按数组的字段之一过滤数组,可以使用:

    {% assign sortedPosts = site.posts | sort: 'title' %}

然后 sortedPosts 变量将包含排序后的数组。

该文档可以在这里找到:https://docs.shopify.com/主题/液体/过滤器/数组过滤器#sort

According to the documentation, to filter an array by one of its field, you can use :

    {% assign sortedPosts = site.posts | sort: 'title' %}

Then the sortedPosts variable will contain the sorted array.

The documentation can be found here : https://docs.shopify.com/themes/liquid/filters/array-filters#sort

溺ぐ爱和你が 2025-01-04 15:17:38

无需插件即可在 GitHub 页面中对 Jekyll 进行排序,既干净又优雅。使用 _data 目录中的 .yml 数据文件。我在这里使用一个名为 team-members.yml 的数据文件:

{% assign sorted_team = site.data.team-members | sort:'title' %}
{% for member in sorted_team %}
    <span class="title">{{ member.title }}</span>
{% endfor %}

此模式将处理您需要在此处执行的操作。

It's both clean and elegant to sort in Jekyll in GitHub pages without a plugin. Use your .yml data file in the _data directory. I use a data file here called team-members.yml:

{% assign sorted_team = site.data.team-members | sort:'title' %}
{% for member in sorted_team %}
    <span class="title">{{ member.title }}</span>
{% endfor %}

This pattern will handle what you need to do here.

谷夏 2025-01-04 15:17:38

我改编了 https://gist.github.com/3812259 中的 Jekyll 插件来完成此任务。我无法按原样使用该插件,因为它在存在空值时失败。我是一名初级 ruby​​ 程序员,并在 https://stackoverflow.com/a/808721/1135052< 的帮助下编写了空处理代码/a>

sort_for 例如反转排序并执行区分大小写的字符串比较(如果排序的属性不是字符串则忽略):

{% sorted_for node in site.pages reversed sort_by:title case_sensitive:true %}
  {{ node.title }}
{% endsorted_for %}

sorted_keys_for 例如:

{% sorted_keys_for tag in site.tags %}
  <a href="/tags/{{ tag | downcase | replace:" ","-"}}.html">{{ tag }}</a><br />
  Num posts: {{ site.tags[tag].size }}
{% endsorted_keys_for %}

用于Jekyll,将此代码放入 _plugins/sort_for.rb 中

module Jekyll
  module SortedForImpl
    def render(context)
      sorted_collection = collection_to_sort context
      return if sorted_collection.empty?
      sort_attr = @attributes['sort_by']
      case_sensitive = @attributes['case_sensitive'] == 'true'
      i = sorted_collection.first

      if sort_attr != nil
        if i.to_liquid[sort_attr].instance_of? String and not case_sensitive
          sorted_collection.sort_by! { |i|
            k = i.to_liquid[sort_attr]
            k ? k.downcase : ''
          }
        else
          sorted_collection.sort_by! { |i|
            k = i.to_liquid[sort_attr]
            [k ? 1 : 0,k || 1]
          }
        end
      else
        if i.instance_of? String and not case_sensitive
          sorted_collection.sort_by! { |i| i.downcase }
        else
          sorted_collection.sort!
        end
      end

      original_name = @collection_name
      result = nil
      context.stack do
        sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
        context[sorted_collection_name] = sorted_collection
        @collection_name = sorted_collection_name
        result = super
        @collection_name = original_name
      end
      result
    end
  end

  class SortedForTag < Liquid::For
    include SortedForImpl

    def collection_to_sort(context)
      return context[@collection_name].dup
    end

    def end_tag
      'endsorted_for'
    end
  end

  class SortedKeysForTag < Liquid::For
    include SortedForImpl

    def collection_to_sort(context)
      return context[@collection_name].keys
    end

    def end_tag
      'endsorted_keys_for'
    end
  end
end

Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)
Liquid::Template.register_tag('sorted_keys_for', Jekyll::SortedKeysForTag)

I adapted a Jekyll plugin from https://gist.github.com/3812259 to accomplish this. I couldn't use the plugin as-is, because it failed in the presence of null values. I'm a beginning ruby programmer, and coded the null handling with help from https://stackoverflow.com/a/808721/1135052

sort_for example reversing the sort and performing case-sensitive string comparisons (ignored if the sorted property is not a string):

{% sorted_for node in site.pages reversed sort_by:title case_sensitive:true %}
  {{ node.title }}
{% endsorted_for %}

sorted_keys_for example:

{% sorted_keys_for tag in site.tags %}
  <a href="/tags/{{ tag | downcase | replace:" ","-"}}.html">{{ tag }}</a><br />
  Num posts: {{ site.tags[tag].size }}
{% endsorted_keys_for %}

For use in Jekyll, put this code in _plugins/sort_for.rb

module Jekyll
  module SortedForImpl
    def render(context)
      sorted_collection = collection_to_sort context
      return if sorted_collection.empty?
      sort_attr = @attributes['sort_by']
      case_sensitive = @attributes['case_sensitive'] == 'true'
      i = sorted_collection.first

      if sort_attr != nil
        if i.to_liquid[sort_attr].instance_of? String and not case_sensitive
          sorted_collection.sort_by! { |i|
            k = i.to_liquid[sort_attr]
            k ? k.downcase : ''
          }
        else
          sorted_collection.sort_by! { |i|
            k = i.to_liquid[sort_attr]
            [k ? 1 : 0,k || 1]
          }
        end
      else
        if i.instance_of? String and not case_sensitive
          sorted_collection.sort_by! { |i| i.downcase }
        else
          sorted_collection.sort!
        end
      end

      original_name = @collection_name
      result = nil
      context.stack do
        sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
        context[sorted_collection_name] = sorted_collection
        @collection_name = sorted_collection_name
        result = super
        @collection_name = original_name
      end
      result
    end
  end

  class SortedForTag < Liquid::For
    include SortedForImpl

    def collection_to_sort(context)
      return context[@collection_name].dup
    end

    def end_tag
      'endsorted_for'
    end
  end

  class SortedKeysForTag < Liquid::For
    include SortedForImpl

    def collection_to_sort(context)
      return context[@collection_name].keys
    end

    def end_tag
      'endsorted_keys_for'
    end
  end
end

Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)
Liquid::Template.register_tag('sorted_keys_for', Jekyll::SortedKeysForTag)
后来的我们 2025-01-04 15:17:38

我想添加以下内容以供将来参考。

要按标题对帖子进行排序,您可以使用 sort 过滤器。
请参阅 http://jekyllrb.com/docs/templates/#filters

所以,这有效:

{% assign sorted_threat_posts = site.categories.threat | sort: 'title', 'last' %}
{% for post in sorted_threat_posts %}
   <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

I wanted to add following for future reference.

To sort posts by title, you can use sort filter.
See http://jekyllrb.com/docs/templates/#filters

So, this works:

{% assign sorted_threat_posts = site.categories.threat | sort: 'title', 'last' %}
{% for post in sorted_threat_posts %}
   <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文