我正在尝试在我的Jekyll网站中创建一个页面,该页面将显示一个自定义变量并列出包含该自定义变量的帖子.
我使用模板Thiago Rossener创建了一个电影评论博客:
蒂亚戈的模板: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
解决方法
标签页使用site.tags,这是一个按标签分组的site.posts数组,由Jekyll在生成时创建.
您试图通过定位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 %}
提示:您可以使用inspect过滤器来调试变量. {{myvar |检查}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。