2

我使用由 Sylvain Durand制作的 Jekyll 多语言设置来运行多种语言的 Jekyll 博客,而无需使用任何插件。

所有帖子都有以下标记:

---
title: Hello world!
lang: en
ref: hello
---

这些帖子使用正常的文件夹结构:

jekyll
|
 -- posts
   |
   --name-of-post
   --name-of-post-2
   --name-of-post-3

我有一个名为en.md的页面,其中包含layout: home和标记,可以正确显示英文帖子,并在home.htmllang: en中使用以下代码

{% assign posts=site.posts | where:"lang", page.lang %}
<ul>
{% for post in posts %}
    <li>
        <a href="{{ post.url }}">{{ post.title }}</a>
    </li>
{% endfor %}
</ul>

但我想按类别显示帖子,按语言过滤。

试图通过以下方式实现这一目标:

  {% assign posts=site.categories | where:"lang", page.lang %}
  <div class="categories">
  {% for category in site.categories %}
  <li><a name="{{ category | first }}">{{ category | first }}</a>
    <ul>
    {% for posts in category %}

        {% for post in posts %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
  {% endfor %}
  </div>

当我构建时,显示以下消息

Liquid 异常:在 /_layouts/home.html 中没有将 String 隐式转换为 Integer

尝试了许多变体,但似乎没有一个有效。

4

2 回答 2

3

这可以解决问题:

---
Title: English posts
lang: en
---
<ul>
{% for category in site.categories %}

  {% comment %}
  Here we have something like this
  category = Array[
    "category1",
    [doc1, doc2]
  ]
  {% endcomment %}

  {% assign name = category[0] %}
  {% assign posts = category[1] %}

  {% comment %}
    >> This also works
    {% assign name = category.first %}
    {% assign posts = category.last %}
  {% endcomment %}

  {% comment %}
    >> Filtering posts based on their `lang` variable
    >> and on the current `page.lang`
  {% endcomment %}
  {% assign selectedPosts = posts | where:"lang", page.lang %}

  {% comment %}
    >> Let's make sure that we need to print something
  {% endcomment %}
  {% if selectedPosts.size > 0 %}
    <li>
      Category {{ name }} :
      <ul>
        {% for post in selectedPosts %}
        <li><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
    </li>
  {% endif %}
{% endfor %}
</ul>

精简版 :

<ul class="post-list">
{% for c in site.categories %}
  {% assign selectedPosts = c.last | where:"lang", page.lang %}
  {% if selectedPosts.size > 0 %}
    <li>Category {{ c.first }} :
      <ul>
        {% for post in selectedPosts %}
        <li><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }} - {{ post.lang }}</a></li>
        {% endfor %}
      </ul>
    </li>
  {% endif %}
{% endfor %}
</ul>
于 2018-10-25T06:46:36.453 回答
1

使用 Jekyll 的内置类解决方案

我找到了这个部分解决方案,就像你做的一样......:

{% for category in site.categories %}
  <li><a name="{{ category | first }}">{{ category | first }}</a>
    <ul>
    {% for posts in category %}
      {% for post in posts %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}

您想要做/做的是使用页面语言过滤“帖子”变量。这确实可以通过assign使用where过滤器来完成,但应该看起来像这样(因为“lang”是单个帖子的属性,而不是类别的属性):

{% assign lang_posts = posts | where:"lang", page.lang %}

这导致以下代码:

{% for category in site.categories %}
  <li><a name="{{ category | first }}">{{ category | first }}</a>
    <ul>
    {% for posts in category %}
      {% assign lang_posts = posts | where:"lang", page.lang %}
      {% for post in lang_posts %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}

没有 Jekyll 的内置类解决方案

如果您在前面的内容中有一个随机的类别列表,如下所示:

- categories:
  - web
  - css
  - internet

...并且您的网站_config.yml包含一个类似(但更完整)的列表,如下所示:

- categories:
  - web
  - css
  - internet
  - html5
  - jekyll

你还有另一个问题。在这种情况下,您没有使用 Jekyll 的内置类别解决方案,并且此解决方案不适用。在这种情况下,许多语句的含义不同,例如:

{% for category in site.categories %}
  {{ category | first }}

这意味着您遍历所有现有的类别_config.yml,然后category | first应该是category。在这种情况下,您可能想要这样的东西:

<ul class="categories">
{% for category in site.categories %}
  <li><a name="{{ category }}">{{ category }}</a>
    <ul>
    {% assign posts=site.posts | where:"lang", page.lang %}      
    {% for post in posts %}
      {% if post.categories contains category %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endif %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}
</ul>

请注意,这只是覆盖 Jekyll 类别变量的简单 Jekyll/liquid 数组逻辑。

于 2018-10-23T08:13:51.417 回答