我正试图在Jekyll网站上创建一个页面,该页面将显示一个自定义变量并列出包含该自定义变量的帖子。
我使用Thiago Rossener创建的模板创建了电影评论博客: Thiago的模板:https://github.com/thiagorossener/jekflix-template 我的网站:https://www.howdareyoureview.com/
在每篇文章中,我都在YAML前端定义了与电影详细信息(即演员,导演评分等)相关的自定义变量
例如:
---
layout: post
title: "Baby Driver"
image: 'https://res.cloudinary.com/how-dare-you-review/image/upload/c_fill,h_399,w_760/v1529865791/baby-driver.png'
tags:
- action
score: 72
director: Edgar Wright
written-by: Edgar Wright
staring:
- Ansel Elgort
- Lily James
- Eiza González
- Jon Hamm
- Jamie Foxx
---
我想创建与该模板中已经存在的标签页面完全相同的页面: https://www.howdareyoureview.com/tags/
除了我想按导演,主演等进行排序,而不是按标签进行排序。 标签页是使用以下代码在tags.html文件中创建的:
---
layout: minimal
title: "#Tags"
permalink: /tags/index.html
description: "Procure por sua #tag favorita."
---
<div class="tags">
{% assign tags_list = site.tags %}
{% if tags_list.first[0] == null %}
{% for tag in tags_list %}
<a href="#{{ tag | slugify }}">{{ tag }}</a>
{% endfor %}
{% else %}
{% for tag in tags_list %}
<a href="#{{ tag[0] | slugify }}">{{ tag[0] }}</a>
{% endfor %}
{% endif %}
{% assign tags_list = nil %}
</div>
{% for tag in site.tags %}
<div class="tag-wrapper">
<span class="tag-title" id="{{ tag[0] | slugify }}">{{ tag[0] }}</span>
<ul class="post-list">
{% assign pages_list = tag[1] %}
{% for post in pages_list reversed %}
{% if post.title != null %}
{% if group == null or group == post.group %}
<li><a href="{{ site.url }}{{ post.url }}">{{ post.title }}<span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%m/%d/%Y" }}</time></a></li>
{% endif %}
{% endif %}
{% endfor %}
{% assign pages_list = nil %}
{% assign group = nil %}
</ul>
</span>
</div>
{% endfor %}
为实现我创建的自定义变量,我尝试用Director替换“ tag / tags”,并将文件另存为“ directors.html”到根目录,但页面空白。
---
layout: minimal
title: "#Directors"
permalink: /directors/index.html
description: "Procure por sua director favorita."
---
<div class="directors">
{% assign directors_list = site.director %}
{% if directors_list.first[0] == null %}
{% for director in directors_list %}
<a href="#{{ tag | slugify }}">{{ director }}</a>
{% endfor %}
{% else %}
{% for director in directors_list %}
<a href="#{{ director[0] | slugify }}">{{ director[0] }}</a>
{% endfor %}
{% endif %}
{% assign directors_list = nil %}
</div>
{% for director in site.director %}
<div class="director-wrapper">
<span class="director-title" id="{{ tag[0] | slugify }}">{{ director[0] }}</span>
<ul class="post-list">
{% assign pages_list = director[1] %}
{% for post in pages_list reversed %}
{% if post.title != null %}
{% if group == null or group == post.group %}
<li><a href="{{ site.url }}{{ post.url }}">{{ post.title }}<span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%m/%d/%Y" }}</time></a></li>
{% endif %}
{% endif %}
{% endfor %}
{% assign pages_list = nil %}
{% assign group = nil %}
</ul>
</span>
</div>
{% endfor %}
由于代码和概念与标记的填充方式完全相同-我无法理解为什么这样做不起作用-我希望有人可以提供帮助!
这是我的整个目录供参考: https://github.com/howdareyoureview/howdareyoureview.github.io
答案 0 :(得分:1)
“标签”页面使用site.tags
,它是由Jekyll在生成时创建的,按标签分组的site.posts
的数组。
您正试图通过定位site.directors
进行复制,但是此预期的数组不存在。但是,您可以使用group_by过滤器来实现您的目标。
<div class="directors">
{% assign directors = site.posts | group_by: 'director' | sort: "name" %}
{% for director in directors %}
{% if director.name == "" %}
{% assign name = "Anonymous" %}
{% else %}
{% assign name = director.name %}
{% endif %}
<a href="#{{ name | slugify }}">{{ name }}</a>
{% endfor %}
</div>
{% for director in directors %}
<div class="director-wrapper">
{% if director.name == "" %}
{% assign name = "Anonymous" %}
{% else %}
{% assign name = director.name %}
{% endif %}
<span class="director-title" id="{{ name | slugify }}">{{ name | debug }}</span>
<ul class="post-list">
{% assign pages_list = director.items %}
{% for post in pages_list reversed %}
<li><a href="{{ site.url }}{{ post.url }}">{{ post.title }}<span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}" itemprop="datePublished">{{ post.date | date: "%m/%d/%Y" }}</time></a></li>
{% endfor %}
</ul>
</span>
</div>
{% endfor %}
提示:您可以使用检查过滤器调试var。 {{ myvar | inspect }}