英文:
Jekyll - multiple words in the feature tag: {% for post in site.tags.featured %}
问题
在Jekyll的.md文章标题中,我有以下原始的featured标签,我需要修改为top featured:
layout: post
title:
author:
categories: Jekyll
image:
tags: featured
在Jekyll的sidebar-featured.html页面中,我有以下的HTML + Liquid代码:
<ol class="list-featured">
{% for post in site.tags.featured %}
<li class="mb-4">
如果我使用tags: top-featured和**{% for post in site.tags.top-featured %}**,一切都能正常工作。
Jekyll是否提供一种快速的方法,可以在Liquid代码中使用类似**{% for post in site.tags.<硬编码的部分>%}的方式,并保持两个单词top featured**之间的空格?
英文:
In the header of Jekyll .md posts I have the following original featured tag which I need to amend to top featured :
layout: post
title:
author:
categories: Jekyll
image:
tags: featured
In the Jekyll sidebar-featured.html page I have the following html + liquid code:
<ol class="list-featured">
{% for post in site.tags.featured %}
<li class="mb-4">
If I use tags: top-featured and {% for post in site.tags.top-featured %} everything works like a charm.
Does Jekyll offer any quick way for using something like {% for post in site.tags.<hardCoded> %} in the liquid code, and mantain the empty space between the two words top featured ?
答案1
得分: 1
我建议在数组表示法中使用标签名称(参见https://stackoverflow.com/q/40117564/10655742),将
---
layout: post
categories: Jekyll
tags: featured
---
更改为
---
layout: post
categories: Jekyll
tags: [ featured ]
---
这样你就可以在标签中使用spaces
---
layout: post
categories: Jekyll
tags: [ featured, my amazing tag ]
---
出于调试目的,你可以“记录”整个站点的标签。
## 列出站点的所有标签
<ul>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<li>{{ t }}</li>
{% endfor %}
</ul>
要列出按标签筛选的帖子
## 一个单词标签 `featured`
<ol class="list-featured">
{% for post in site.tags.featured %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
## 两个单词标签 `top featured`
<ol class="list-featured">
{% for post in site.tags["top featured"] %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
最后,当你想要避免标签的硬编码值时,可以使用assign
## 两个单词标签 `top featured`,使用 **assign**
<ol class="list-featured">
{% assign myTagName = "top featured" %}
{% for post in site.tags[myTagName] %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
英文:
I would suggest to use tag-names in array notation (see https://stackoverflow.com/q/40117564/10655742), change
---
layout: post
categories: Jekyll
tags: featured
---
to
---
layout: post
categories: Jekyll
tags: [ featured ]
---
so you can use spaces
in tags
---
layout: post
categories: Jekyll
tags: [ featured, my amazing tag ]
---
For Debugging purpose you can "log" your tags of the entire site.
## List all Tags of site
<ul>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<li>{{ t }}</li>
{% endfor %}
</ul>
To list the filterd posts by tag
## One word Tag `featured´
<ol class="list-featured">
{% for post in site.tags.featured %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
## Two word Tag `top featured´
<ol class="list-featured">
{% for post in site.tags["top featured"] %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
Finnally, use assign when you want avoid hard-coded value of your tag
## Two word Tag `top featured´ with **assign**
<ol class="list-featured">
{% assign myTagName = "top featured" %}
{% for post in site.tags[myTagName] %}
<li class="mb-4">{{ post.title }}</li>
{% endfor %}
</ol>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论