I have the following in my _config.yml file:
collections:
nr_qa:
output: true
permalink: /:collection/:name
title: 'Node-RED Questions and Answers'
descriptions: 'Node-RED is a flow-based (visual) programming tool. These pages have some information that may be currently missing from the documentaiton.'
github_pages:
title: 'GitHub Pages and Jekyll/Liquid'
description: 'Hints and tips on using Jekyll for publishing to GitHub Pages.'
output: true
permalink: /:collection/:name
and I want to create an automatic index for my collections. So I use code like this:
## {{ site.collections.github_pages.title }}
{{ site.collections.github_pages.description }}
<ul>
{% for item in site.github_pages %}
<li>
{{ item.title | replace:'_',' ' }}
<p>{% if item.description %}
{{ item.description }}
{% else %}
{{ item.excerpt | strip_html }}
{% endif %}</p>
</li>
{% endfor %}
</ul>
And yes, I know I've rather mixed up my markdown and html there. Not relevant to this question.
The problem is that {{ site.collections.github_pages.title }} and {{ site.collections.github_pages.description }} don't render anything even though I think they should.
Can anyone point out my mistake please?
The problem is that title and description should be included in each collection, and not in _config.yml.
Check out Accessing Collection AttributesPermalink for further details.
update
title can be present in each collection metadata in _config.yml. The problem is how you are accessing those variables.
One approach is to have a specific layout for each collection, then you can access them like:
{% assign col = site.collections | where: 'label','github_pages' | first%}.
TITLE: {{ col.title }}.
DESCRIPTION: {{ col.description }}.
Related
when I create the port on Jekyll admin
title: layout:
page comments: true
social-share: true
show-avatar:true
use_math: true
I want to create this as default how should I do it?
Copy paste into your new post. Most CMS editors do that for you (like CloudCannon).
The real solution would be to transform them into site variables, by adding them to _config.yml and calling them using {{ site.social-share }} instead of {{ page.social-share }}.
An advanced solution would be to check if there are page variables, like this:
{% if page.social-share != nil %}
{{ page.social-share }}
{% else %}
{{ site.social-share }}
{% endif %}
Short Version:
Why does the following code not produce an output when navbox.next_article is the string '2018-01-05-man-command'?!
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
Details
My post 2018-01-05-man-command.md has a YAML front matter:
---
layout : post
title : 'Man Command'
tags : [RHCSA, RHCSA_mod, Using Essential Tools, Man Command]
categories: [RHCSA]
navbox:
# prev_article:
next_article: 2018-01-05-understanding-globbing-and-wildcards
---
This is accessed by the _includes/post.html file through:
{% unless include.excerpt %}
{{ post.content }}
{% include navbox.html navbox=page.navbox %}
{% endunless %}
This is used by the _layout/post.html which sets the layout for the post:
{% include post.html post=page link_title=false %}
My navbox.html contains:
{% assign navbox = include.navbox %}
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
However, all I get when I run bundle exec jekyll serve is:
The next post is
Why does that line not work? I'm new to jekyll so it's possible I've made a blunder somewhere that's intuitive to most. Please tell me what I can fix.
I believe that the capture tag only captures strings, not posts. See here for more info.
I'm not convinced that a where filter supports the contains syntax you're using. See here for more info.
On top of that, where returns an array. You have to get the first item from that array.
You need to fix these issues. Use an assign instead of a capture to store a post. And change your where filter to not use the contains syntax, which isn't valid. (Unless it's been added since the issue I just linked.)
Here is how I've done it:
{% assign post = site.posts | where:"url", targetUrl | first %}
I have a Jekyll collection which is not being output, but elements of which are being displayed on a single page, like this:
{% for element in site.collection %}
{{ element.content }}
{% endfor %}
I would like to be able to do something like this:
{% for element in site.collection %}
{{ element.content }}
{% for front_matter in element %}
<!-- do stuff -->
{% endfor %}
{% endfor %}
This is possible with YAML hashes in YAML _data files, but doesn't work here because, it seems {{ element }} on its own is identical to {{ element.content }}. There doesn't seem to be any variable designated for this either like, {{ element.front_matter }}. Is it possible to loop through the front matter of an element in a jekyll collection?
I know that the ideal way to do this would be to include all the front_matter I want to loop through in a variable, like:
---
front_matter:
foo: bar
bar: foo
---
But, as I'm trying to configure these pairings (foo and bar) to be easily updatable through prose.io, they can't be nested under other values. If there is a
way around this with prose though, I would accept that answer.
Much appreciated!
It is possible to loop through the variables of an element in a Jekyll collection:
{% for items in site.my_collection %}
{% for item in items %}
{{ item }}: {{ items[item] }}
{% endfor %}
{% endfor %}
But it is important to remember that there is other metadata that is also available and will be included in the iteration, like path, url, etc and your front matter, e.g. for _my_collection/something.md:
next:
path: _my_collection/something.md
output: <p></p>
url: /my_collection/something.html
id: /my_collection/something
content: <p></p>
collection: my_collection
relative_path: _my_collection/something.md
excerpt: <p></p>
previous:
draft: false
categories:
slug: something
ext: .md
tags:
date: 2017-05-23 14:43:57 -0300
I'm building a magazine site with Jekyll (v2.5.3). The docs on the Jekyll site led me to believe that I could list all the collections on my site, and embed YAML data for each collection in my _config.yml.
_config.yml:
collections:
issue_001:
output: true
permalink: /:title/:path
title: Rebirth
date: 2015-07-01
issue_002:
output: true
permalink: /:title/:path
title: Talking Heads
date: 2015-08-01
index.html:
{% for issue in site.collections %}
<li>
<h6 class="post-meta">Issue {{ issue.name }} — {{ issue.date | date: "%b %-d, %Y" }}</h6>
<h2>
{{ issue.title }}
</h2>
</li>
{% endfor %}
I get two issues appearing on the homepage, but none of the data I'm accessing for each issue (name, date, title etc.) is appearing. I appreciate this is a beta feature, so just wanted to ask is this broken, or am I doing it wrong?
In case for someone still has this request here's how it works in latest Jekyll (v3.8.3):
Update: tested it still works in v4.2.0
{% for collection in site.collections %}
<h2>Items from {{ collection.label }}</h2>
<ul>
{% for item in site[collection.label] %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% endfor %}
In {% for issue in site.collections %}, issue is an array that contains :
0 => "issue_001",
1 => Hash
{"output"=>true,
"permalink"=>"/:title/:path",
"title"=>"Rebirth",
"date"=>#,
"label"=>"issue_001",
"docs"=>[#],
"files"=>[],
"directory"=>"/home/djacquel/www/test.dev/jekyll/wat/_issue_001",
"relative_directory"=>"_issue_001"}
The right way to access datas is :
{% for issue in site.collections %}
<li>
<h6 class="post-meta">
Issue {{ issue[1].label }}
—
{{ issue[1].date | date: "%b %-d, %Y" }}
</h6>
<h2>
{{ issue[1].title }}
</h2>
</li>
{% endfor %}
Second first; that answer from #sparanoid is fantastic! And between that and the source code from the default Jekyll Theme (Minima), I was able to bodge some of what follows together.
First second; excellent question and how both are not up-voted more is a bit baffling.
And third of many other related points; I'm not on a identical path as the question's poster but the following code may be of use to readers. My use case was wanting a way of listing the continence of a collection similar to the default Jekyll home layout.
_layouts/named_collection.html
Note the source links are downloadable via, clicking on the Raw link/button then Ctrl s to save as a regular text file, however, these links have been frozen in time so be sure to check the gh-pages branch for updates. And output examples are intended in this case to be just examples of possible content, eg. it could be Lorem strings for all that it matters to demonstrate what's to follow.
---
layout: default
---
{% comment %}
License note from S0AndS0; an editor in this context.
Minima (where the following code is sourced from), is shared under the
MIT Lisense, https://github.com/jekyll/minima/blob/master/LICENSE.txt
Thus any alterations to Minima source code is re-shared under the MIT Lisense
{% endcomment %}
{% capture workspace_collections %}
{% assign collection_name = page.collection_name | default: include.collection_name | default: nil %}
{% assign target_collection = site[collection_name] %}
<div class="home">
{%- if page.title -%}
<h1 class="page-heading">{{ page.title }}</h1>
{%- endif -%}
{{ content }}
{% assign has_output = False %}
{%- if target_collection.size > 0 -%}
{% capture workspace_collection %}
<h2 class="post-list-heading">{{ page.list_title | default: "Posts" }}</h2>
<ul class="post-list">
{%- for post in target_collection -%}
{%- if page.relative_path == post.relative_path -%}
{%- continue -%}
{%- else -%}
{% assign has_output = True %}
{%- endif -%}
<li>
{%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
<span class="post-meta">{{ post.date | date: date_format }}</span>
<h3>
<a class="post-link" href="{{ post.url | relative_url }}">
{{ post.title | escape }}
</a>
</h3>
{%- if site.show_excerpts -%}
{{ post.excerpt | markdownify | remove: '<p>' | remove: '</p>' }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{% endcapture %}
{%- if has_output == True -%}
{{- workspace_collection -}}{% assign workspace_collection = nil %}
{%- endif -%}
{%- endif -%}
</div>
{% endcapture %}{{ workspace_collections | strip }}{% assign workspace_collections = nil %}
Side note, I can't remember where I picked up that capture some workspace_ trick, but it's pretty sweat for stripping new lines and other formatting shenanigans. If a reader figures out who started such fanciness, please edit or leave a comment.
Usage of this layout is much like the usage of Minima's home.html layout.
administration.md
... Front-Matter selecting from the _administration collection looks like...
layout: named_collection
collection_name: administration
title: Administration
list_title: Usage Examples
permalink: /admin/
... resulting in a <base-url>/admin/ collection listing being output.
git_shell_commands.md
... Front-Matter selecting from the _git_shell_commands collection looks like...
layout: named_collection
collection_name: git_shell_commands
title: Git Shell Commands
list_title: Usage Examples
permalink: /git_shell_commands/
... resulting in a <base-url>/git_shell_commands/ collection listing being output.
Even more notes on collections
If I'm reading the latest Jekyll Collections documentation correctly, as of version 3.7.0 it could be possible to have a custom collections directory, in the case of this question that might look like...
ls ~/git/magazine-name/
# ... other files and/or directories
# issues/
# ... other files and/or directories
# _config.yml
... where the issues/ directory contains sub-directories such as...
ls ~/git/magazine-name/issues/
# _001/
# _002/
# _003/
# ...
... and the _config.yml file has...
collections_dir: issues
collections:
001:
output: True
# ...
... the magic sauce was collections_dir and not using an under-scored prefixed base directory. The documentation was loud about that last bit, eg. _issues/ would make for a bad time.
I'm not sure if there'd be any other adjustments needed to the previously posted Liquid code (I don't think so, but probably); so maybe try'em out individually first.
Warning/Update
Jekyll does not take kindly to numbered collections, eg. _000 will cause errors!
To mitigate such issues be sure to trick Jekyll into treating collection names as strings, eg. _i000
The reason I suggest using a collections_dir, is because in the case of #James Dinsdale's question it may help in organizing things a bit more. Personally I wanted my gh-pages branch collection to mirror some of the same paths as my project's master branch, so didn't pursue testing collections_dir suggestions.
Notes about my Liquid coding choices
Because I'm using the Minima theme I placed the files that used the named_collection theme in my project's root directory; default behavior is to link to such pages at the top of every page. Cheap navigation.
Because readers might place pages that use the named_collection within the same directory as what is being listed, eg...
ls Jekyll_Admin/_administration/
# _administration/administration.md
# ...
... I've written the Liquid for some of the edge-cases, but may not have covered all of'em. Be sure to test privately before unleashing something like it on production.
Updates
After some testing with collections_dir it looks like when used the _posts directory must be moved to that directory too, eg...
_config.yml (Snip)
collections_dir: misc
Bash commands
mkdir misc
mv _posts misc/
... though that's only applicable if using a _posts directory.
Between the time of the last edit and when you're now reading this I've published liquid-utilities/collection-home on GitHub; might be of use to those that want a version tracked solution and is what I'm currently using in any projects that require this feature.
I have a network of nodes, some of which are related to one another. I'm using Jekyll to power a website, and was hoping to use liquid tags to map those relationships.
So, the way I've been trying to do this is as follows.
A, which is in the category of 1, is related to B
B, which is in the category of 2, is related to A, and C
When I visit the page for A, I'd like to see B listed as being related.
I've defined the YAML front matter as such:
title: A
category: 1
tags:
- B.html
title: B
category: 2
tags:
- A.html
- C.html
My liquid template looks like:
<h2>{{ page.title }} <span class="label important">{{ page.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>
<h4>Related</h4>
<ul>
{% for post in site.tag.{{ post.url }} %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
To me, that looks like it should work. In reality, it doesn't.
Suggestions are welcome!
Also, relevant Github pages are here:
https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-bycatch.md
https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-accidental.md
https://github.com/salmonhabitat/salmonhabitat.github.com/blob/master/_layouts/post.html
What I was intending to happen was for 'Accidental injury' to show up under 'Marine bycatch's' Related nodes...
The first problem is that posts are basically "blind" to other posts in jekyll. It's impossible to the url (or title) of one post from inside another post in jekyll, with only the id of the former. Your site.tag.{{ post.url }}, while creative, would not work :) .
First, your front matter needs to be (unfortunately) a bit more complicated to be able to accomplish that:
title: A
category: 1
related_posts:
- title: B
href: /2010/01/01/B.html
- title: C
href: /2011/11/11/C.html
Notice that I've changed the name from "tags" to "related_posts". I feel it's more clear this way.
Now you can try this code:
<h2>{{ post.title }} <span class="label important">{{ post.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>
{% if post.related_posts %}
<h4>Related</h4>
<ul>
{% for related_post in post.related_posts %}
<li>{{ related_post.title }}</li>
{% endfor %}
</ul>
{% endif %}
While this is a bit more verbose than your version, it has an advantage - you can specify your own title in related posts, without being forced to use B or C.
Let me know if you have problems with this, and good luck!