Jekyll – 特性标签中的多个词语: {% for post in site.tags.featured %}

huangapple go评论51阅读模式
英文:

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:

&lt;ol class=&quot;list-featured&quot;&gt;
    {% for post in site.tags.featured %}   
        &lt;li class=&quot;mb-4&quot;&gt;

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
&lt;ul&gt;
    {% for tag in site.tags %}
        {% assign t = tag | first %}
        {% assign posts = tag | last %}

        &lt;li&gt;{{ t }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

To list the filterd posts by tag

## One word Tag `featured&#180;
&lt;ol class=&quot;list-featured&quot;&gt;
    {% for post in site.tags.featured %}
        &lt;li class=&quot;mb-4&quot;&gt;{{ post.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ol&gt;

## Two word Tag `top featured&#180;
&lt;ol class=&quot;list-featured&quot;&gt;
    {% for post in site.tags[&quot;top featured&quot;] %}
        &lt;li class=&quot;mb-4&quot;&gt;{{ post.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ol&gt;

Finnally, use assign when you want avoid hard-coded value of your tag

## Two word Tag `top featured&#180; with **assign**
&lt;ol class=&quot;list-featured&quot;&gt;
    {% assign myTagName = &quot;top featured&quot; %}
    {% for post in site.tags[myTagName] %}
        &lt;li class=&quot;mb-4&quot;&gt;{{ post.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ol&gt;

huangapple
  • 本文由 发表于 2023年2月24日 16:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定