How to add new posts like page in Jekyll? - ruby

I am setting up a GitHub page which uses Jekyll. I know how to create a new post, new page. I wanted a new "posts" like the page, wherein I can add posts which I want to. So there will be a posts page (by default) and there will be some other page say blog, both of which shows some posts in the appropriate category.

You can create a page which lists all posts which have a certain category or tag.
Example code from the link:
---
layout: page
---
{% for post in site.categories[page.category] %}
<a href="{{ post.url | absolute_url }}">
{{ post.title }}
</a>
{% endfor %}
If the .md files you are talking about aren't posts, you can use Collections.
Here's example code from the link tailored to your xyz example - basically, you define your collection in the config file:
collections:
- xyz
Then, you create .md files in an _xyz folder, and you can display a list of them like this:
{% for item in site.xyz %}
<h2>{{ item.title }}</h2>
<p>{{ item.description }}</p>
<p><a href = "{{ item.url }}" >{{ item.title }}</a></p>
{% endfor %}

Related

How can I specify an 11ty collection in frontmatter?

On my website, I want to show a list of collection items on pages corresponding to each collection. For example on the Games page, I want to show a list of all game related articles, on the Politics page, I want to show a list of all politics related articles, etc.
What I have right now is one Nunjucks template file for each such page, which I don't particularly like since they are the same except for the collection to show. Here is two of my templates, for my politics articles and for my games articles, where I extend a common boilerplate (base.njk) and in the main block, I first print some Markdown content and then comes the list of collection items:
{# politics.njk #}
{% extends "base.njk" %}
{% block main %}
<main>
{{ content | safe }}
<ul class="postlist no-bullets">
{% for item in collections.politics %}
{% include "partials/collection-list-item.njk" %}
{% endfor %}
</ul>
</main>
{% endblock %}
{# games.njk #}
{% extends "base.njk" %}
{% block main %}
<main>
{{ content | safe }}
<ul class="postlist no-bullets">
{% for item in collections.games %}
{% include "partials/collection-list-item.njk" %}
{% endfor %}
</ul>
</main>
{% endblock %}
As you can see, the only difference between these two files is collections.politics and collections.games. I would want to use one template, and in the frontmatter specify which collection I want. Since I'm already having one Markdown file per page the template used (for writing stuff before I begin listing articles), it would have been nice if it was possible. For example:
<!-- politics.md -->
---
layout: articles-list.njk
title: Politics
listCollection: collections.politics
---
# Politics
Below is a list of all politics articles I've written.
<!-- games.md -->
---
layout: articles-list.njk
title: Games
listCollection: collections.games
---
# Games
I think about games a lot. Below is a list of articles I've written on the topic.
{# articles-list.njk #}
{% extends "base.njk" %}
{% block main %}
<main>
{{ content | safe }}
<ul class="postlist no-bullets">
{% for item in listCollection %}
{% include "partials/collection-list-item.njk" %}
{% endfor %}
</ul>
</main>
{% endblock %}
Is it possible to do what I want to do in some other way than to have multiple template files?
You might be able to get to the collection using computed data, though I'm not sure if you have access to the collections there.
A simpler approach: Set your frontmatter field to the name of the colletion you want to display as a string, then use that to get the collection in the template:
<!-- politics.md -->
---
layout: articles-list.njk
title: Politics
listCollection: 'politics'
---
{# articles-list.njk #}
{% extends "base.njk" %}
{% block main %}
<main>
{{ content | safe }}
<ul class="postlist no-bullets">
{% for item in collections[listCollection] %}
{% include "partials/collection-list-item.njk" %}
{% endfor %}
</ul>
</main>
{% endblock %}
The way I handled this on my blog (https://github.com/cfjedimaster/raymondcamden2020) was like so:
I create a collection of my categories. I do this by using .eleventy.js and eleventyConfig.addCollection. I use some JavaScript to get all of my posts, iterate over each ones list of categories, and creating a unique list. At the end, I've got a collection called categories.
eleventyConfig.addCollection("categories", collection => {
let cats = new Set();
let posts = collection.getFilteredByGlob("_posts/**/*.md");
for(let i=0;i<posts.length;i++) {
for(let x=0;x<posts[i].data.categories.length;x++) {
cats.add(posts[i].data.categories[x].toLowerCase());
}
}
return Array.from(cats).sort();
});
I then made one page, categories.liquid, that is reponsible for generating my category pages. It's rather simple:
---
pagination:
data: collections.categories
size: 1
alias: cat
permalink: "categories/{{ cat | myEscape }}/"
layout: category
renderData:
title: "{{ cat }}"
---
Notice I use cat for each item in the category and notice I've got nothing else in here. All the layout is done in the category layout page.
Here's the top portion of my category page:
---
layout: default
---
{% assign posts = collections.posts | getByCategory: cat %}
So given I've got a collection of posts where each post has N categories assigned to it, I wrote a custom filter that just reduces this down to a post that has cat as one of it's categories.
S the end result is N category pages filled posts assigned to that category.

Create Jekyll link from YAML array

I have a YAML array in a file called navigation.yml as follows:
docs:
- title: Home
url: index.md
id: index
- title: Support
url: support.html
id: support
- title: About
url: about.md
id: about
I am creating a navigation bar as follows:
<section id="navigation" class="clearfix">
{% for item in site.data.navigation.docs %}
<span>{{ item.title }}</span>
{% endfor %}
</section>
What should I put in place of index.md to get the item.url that I want from the YAML file.
I am totally new to GitHub Pages, YAML, and Jekyll.
At the moment, the link tag doesn't seem to support variables.
There's a pull request trying to change this, but it has not been merged into the main Jekyll repo yet.
So if you want to do this now, you need to use some tricks.
The solution suggested by flyx in his comment (replace {% link index.md %} by {{ item.url }}) basically works, but shows the original filename written in the data file.
⇒ If index.md is automatically renamed to index.html while rendering the site, your link won't work anymore.
(or if support.html becomes support/index.html)
That's probably why you wanted to use the link tag instead.
Without using the link tag, you need to loop your data file, loop through all pages to find the respective page, and show that page's actual URL in your link:
<section id="navigation" class="clearfix">
{% for item in site.data.navigation.docs %}
{% for page in site.pages %}
{% if page.path == item.url %}
<span>{{ item.title }}</span>
{% endif %}
{% endfor %}
{% endfor %}
</section>
This even takes stuff like explicitly set permalinks (permalink: /whatever/ in the page's front matter) into account.

Adding a post type to octopress

I am working on a blog for my personal use and wanted to add a news feature in it.
This feature will show five recent news markdown files that I have placed inside the _news folder. But i am unable to understand how to access the directory using the liquid markup in the template just like it is done for posts in the _posts folder.
Try using collections rather than posts. Collections can be iterated through by Jekyll/Liquid using {% for n in site.news %}, for the news collection.
The only way to create a news type of posts is to do it with a plugin.
But I think your problem can be resolved in a simpler manner, using categories or tags.
Here I'll explain tag's use, but it's the same with categories.
A post with a news tag :
---
layout: post
title: "Post 2"
date: 2015-08-12 18:02:44
tags:
- news
- javascript
- anything else
---
Post 2 content
The loop used to get all the news tagged posts :
<ul>
{% for post in site.posts %}
{% if post.tags contains "news" %}
<li>
<h2>
{{ post.title }}
</h2>
</li>
{% endif %}
{% endfor %}
</ul>

How to use Jekyll's pagination tool to sort through a subdirectory within _posts

Having trouble getting pagination to work properly in Jekyll using liquid markdown.
Used the paginator function and the posts do paginate but I'm trying to figure out a way to filter out what paginates.
Within my _posts directory I have a blog sub directory and a news subdirectory. I'm trying to only paginate the articles in blog and not news.
{% assign posts = site.posts | where: 'category','Blog' %}
<ul class="list-unstyled blog-list">
<!-- This loops through the paginated posts -->
{% for post in paginator.posts %}
{% assign posts = site.posts | where: 'category','Blog' %}
{% if post.category == 'Blog' %}
Do Stuff
{% endif %}
{% endfor %}
<!-- Pagination links -->
<div class="pagination">
{% if paginator.previous_page == 1 %}
Previous
{% endif %}
{% if paginator.previous_page > 1%}
Previous
{% endif %}
<span class="page_number ">Page: {{ paginator.page }} of {{ paginator.total_pages }}</span>
{% if paginator.next_page %}
Next
{% endif %}
</div>
</ul>
I was trying to retrieve posts that have the category 'Blog' but when I run it, it paginates to the 7 articles per page I wanted, but some pages have less then 7 articles or no articles show up at all.
I think somethings wrong with my loop logic but I'm not sure what.
Maybe it's grabbing all the articles in _posts regardless but not showing the news articles on the page?
Thanks!
Jekyll Paginate does not have filtering features therefore it's not possible to filter paginated posts by category. I'd recommend You to look into Octopress Paginate plug-in which provides the feature.

Author archive pages in Jekyll

I'm trying to make a multiple author blog with Jekyll on Github pages. I added authors array field to _config.yml and I can use that data on posts template.
_config.yml:
authors:
muratcorlu:
display_name: Murat Corlu
avatar: 2906955ae59c795275979d3782d7bfca
posts.html
{% assign author = site.authors[page.author] %}
<p>Author: {{ author.display_name }}</p>
Now I want to make an author archive page with a url like /authors/muratcorlu/ (i.e. listing posts authored by muratcorlu), but I don't know how can I get author name from url.
I'm afraid you can't create those pages automatically. If you have 5 authors, you will have to create 5 pages manually. The pages can use the same layout, so it will not be very painful.
This would be authors/muratcorlu.textile
---
layout: author
author: muratcorlu
---
You would have to create each of those manually. Fortunately, you don't have to do anything else - the rest can be put in a shared layout that can look like this:
<ul>
{% for p in site.pages do %}
{% if p.author == page.author %}
<li>{{ p.title }}</li>
{% endif %}
{% endfor %}
</ul>

Resources