Hi all, new user here was thinking about setting up a blog with either Jekyll or Hugo.
I am having an issue looping over my categories and output the title of each post from the directory. I’m hopeful there isn’t a simple spelling error / obo as I’ve been staring at this for too many hours at this point.
Repo can be found here
In _configyml
I have the following settings:
# Outputting
permalink: /:collection/:title/
# collections_dir: collections
collections:
ux:
output: true
permalink: /ux/:path
# FE:
# output: true
# permalink: /FE/:path
When I navigate to localhost/[baseurl]/ux/
I get the Index page, and can click on the test.md
file. (baseurl is testdeploy
in this case)
When I do:
{% for collection in site.collections %}
{% assign name = collection.label %}
<h1>{{ name }}</h1>
{% for page in site.name %}
<p>foo</p>
<p>{{ page.title }}</p>
{% endfor %}
{% endfor %}
That appears to be to spec according to the docs:
<ul>
{% for author in site.authors %}
<li>
<h2><a href="{{ author.url }}">{{ author.name }}</a></h2>
<h3>{{ author.position }}</h3>
<p>{{ author.content | markdownify }}</p>
</li>
{% endfor %}
</ul>
Yet I don’t even get foo
on the page.
Sorry if I am missing the obvious, but, what am I missing here?
I expected to see the name of the collection ( in an h1 tag), followed by the titles of each .md
file (from the front matter and in a p tag) in the directory. Why am I not?
Thanks.
Assuming this structure:
_ux/ # note underscore here but not in config
abc.md
def.md
index.md
Start with using site.ux as per site.author in the docs
<ul>
{% for ux_item in site.ux %}
<li>
<h2>
<a href="{{ ux_item.url | relative_url }}">
{{ ux_item.title }}
</a>
</h2>
<p>{{ ux_item.description }}</p>
<p>{{ ux_item.excerpt }}</p>
</li>
{% endfor %}
</ul>
My site which has /shows/
as listing of pages like /shows/2020-12-06-tulbagh.html
-
_pages/shows.md (click Raw button. Note use of
show.excerpt
to pick up text from the first paragraph).
-
_shows directory of .md files
Don’t bother setting permalink for your collection.
collections:
ux:
output: true
permalink: /ux/:path
It will be set for your already.
See my repo.
I just made a Tools collection now on that demo site to help you.
michaelcurrin.github.io/jekyll-blog-demo/tools/
The paths are:
/tools/ # tools.md
/tools/ruby/ # _tools/ruby.md
/tools/jekyll/
I’ve setup _config.yml
with permalink: pretty
globally. So all pages end in /
instead of .html
. Which is ideal for a collection.
I’ve listed all the items on site.tools collection here - see Raw
If you really want to iterate over all collections, you can do this. Which I use on my index.md page. I’ve moved it to an .html
page so that indentation doesn’t turn it into code snippets.
You are the best!
Going to mark this as a solution, as that covers basically every question I came up with on this journey and more. (collection.docs … that is beautiful, and after as much digging should have been more obvious)
The rabbit I was ultimately looking to pull out of the hat was to change this:
{% for collection in site.collections %}
{% assign name = collection.label %}
<h1>{{ name }}</h1>
{% for page in site.name %}
<p>foo</p>
<p>{{ page.title }}</p>
{% endfor %}
{% endfor %}
to this:
{% for dir in site.collections %}
{% assign collection = dir.label %}
<h1>{{ collection }}</h1>
{% for page in site[collection] %}
<a href="{{ site.baseurl }}{{ page.url }}">{{ page.title }}</a>
{% endfor %}
{% endfor %}
I’m not super familiar with liquid (er, obvs) and am not seeing, on either the Shopify or Github docs the difference between “.” and “[ ]” (In fact in my quick-like skim through I didn’t see any references to “[]”'s at all)
So, why does that work?
And thanks a million Michael, I found the band site stumbling through other answers here. Super helpful.
1 Like
Awesome. Glad you found the bit that needed a fixed and had questions answered.
Yeah in Liquid and Ruby and JS, this is literally.
name = "people" # the collection
foo.name
# same as
foo['name']
# returns nothing because you didn't use the variable.
The right way is:
name = "people"
foo[name]
# { docs: [], label: "people", ... }