jekyll - 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 循环的结果?
解决方案
只需将变量名写在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
。
推荐阅读
- r - 如何在 r 中绘制点图,其中一个点表示平均值和误差线
- android - 滑出动画不起作用(Android)
- python - 如何将 argparse 参数传递给 sanic 路由?
- list - Kotlin 中 List.subList 和 slice 的区别
- php - 根据 MIME 类型数组检查 MIME 类型作为字符串
- linux - 绑定到接口的问题,错误号:92。我收到此错误。我在 Windows 中使用开发者模式。如何修复它
- javascript - 从种子生成私钥
- swift - build failed 手动合并代码时无法确定生成的 Core Data 代码生成的文件路径
- .net - 通过 VS 2017 设计器在低 DPI 屏幕上绘制兼容高 DPI 的 Windows 窗体
- html - Flexbox 的宽度超过了它的主容器