首页 > 解决方案 > jekyll/liquid - 如何将变量作为参数传递给 post.title contains 语句?

问题描述

我正在尝试根据标题中包含的主题标签过滤循环的结果。如果手动完成,它会完美运行:

<article class="post-preview">
    <a href="{{ post.url | prepend: site.baseurl | replace: '//', '/' }}">
      {% if post.title contains "#hashtag" %}
      <br>
      <div class="float-left">
      {% include read-time.html %}
      </div>
      <h2 class="post-title">{{ post.title }}</h2>
      {% if post.subtitle %}
      <h3 class="post-subtitle">{{ post.subtitle }}</h3>
      {% else %}
      <h3 class="post-subtitle">{{ post.excerpt | strip_html | truncatewords: 15 }}</h3>
      {% endif %}
    </a>
    <p class="post-meta float-right">Posted by
      {% if post.author %}
        {{ post.author }}
      {% else %}
        {{ site.author }}
      {% endif %}
      on {{ post.date | date: '%B %d, %Y' }}</p><br>
      {% endif %}
  </article>

我想要实现的是能够使用变量而不是字符串:

例如

{% if post.title contains "{{ content }}" %} - (这样它不起作用)

代替

{% if post.title 包含“#hashtag” %}

有没有办法将变量传递给 post.title 包含,或任何其他方式来实现我的目标,即根据标题中的主题标签过滤 for 循环的结果?

标签: jekyllliquid

解决方案


只需将变量名写在contains. 例子:

{% assign a = "Sample text" %}
{% assign b = "text" %}

{% if a contains b %}
    Variable 'a' contains the value of variable 'b'! :)
{% endif %}

在您的情况下,a将是post.title并且b将是content


推荐阅读