Multiple Blogs In Single Jekyll Website - ruby

Is there a way I can have a single Jekyll website have more than one blog? I currently want to have two blogs in one site.

I am the author of the page http://www.garron.me/blog/multi-blog-site-jekyll.html
Considering that you need individual archives pages, and latest post per individual blog. Just use something like this:
Create a file archives-blog-1.html and fill it with:
{% for post in site.posts %}
{% if post.categories contains 'blog1' %}
<div class="post">
<h3 class="title">{{ post.title }}</h3>
<p class="meta">Date: {{ post.date }}</p>
</div>
{% endif %}
{% endfor %}
That will give you a list of all post in blog1, you can do the same for blog2. That page can be anyplace you want.
For the latest post, you can use the same code but enclosed between:
{% for post in site.posts limit:5 %}
....
{% endfor %}
That will give you the lastes 5 posts... I am using this
{% for post in site.posts limit:5 %}
<div class="post">
<ul>
<li>{{ post.title | truncate:200 }} <small>{{ post.date }}</small>
{% if post.summary %}
<p class="entry">{{ post.summary }}</p>
{% endif %}
</li>
</ul>
</div>
{% endfor %}
In my index page. http://www.garron.me/index.html ... under the sub-title (From the blogs)
I am not limiting to any category, so posts from all blogs appear there, you can limit with {% if post.categories contains 'blog1' %}
Hope it helps you.

There's a simpler solution than any of the answers so far.
Folder structure:
- blog1/
- _posts/
- blog2/
- _posts/
Then in the index.html for blog1, use site.categories.blog1 instead of site.posts.
See the documentation for "site.categories" and "page.categories" in https://jekyllrb.com/docs/variables/

I used two separate Jekyll installations to run two blogs on the same domain; if your blogs are going to live in separate root dirs (mine are at / and /photos/), then I'd recommend this approach. I also described how I merged both blogs' sitemap.xml files.

Your best bet would be to look into the data files feature. You can put .markdown files in a separate folder in your source and link to them as you post. This does mean that in order to make a post, you'll need to write a data file entry, but you can host as many "blogs" as you'd like, each with their own folder. Posts will automatically have the folder they're in as the url. I use this method for my own personal blog and portfolio.
Either that, or you may want to look into collections: http://jekyllrb.com/docs/collections/

Adding to #ggarron's answer, there's a short version of filtering by categories:
{% assign filtered_posts = site.posts | where_exp: "post", "post.categories contains 'blog1'" %}
The advantage of assigning to a variable is that one can replace all uses of site.posts with filtered_posts and keep consistency. For example, I have a snippet to get the first post of the list:
{% assign first_post = filtered_posts | first %}
This works as expected.

Related

Merge data with posts in Jekyll

I have a directory _posts where I store articles. I also have a data file _data/teasers.yml that contains information about articles posted on other sites. I'm trying to avoid republishing content when I could include a teaser and link back to the original location of the article.
I'd like to combine both teasers and posts so I can loop through them in chronological order. Is this possible with Jekyll/Liquid?
Example teaser
- title: A harmonious future for profiles and pine
date: 2017-05-09
url: https://www.example.com/articles/a-harmonious-future-for-profits-and-pine/
Example template
{% assign posts = site.posts %}
{% assign teasers = site.data.teasers %}
# how can I combine these???
{% assign stories = posts | concat: teasers %}
{% for story in stories %}
{{ story.title }}
{% endfor %}
I'm using Jekyll 3.2.x on a mac.

How to create sorted list of Pages in Octopress or Jekyll?

I have a Octopress website, which has posts and pages. Now I want to add another category of pages which I want to call as writepus ( notes which I will keep updating like a wiki via git commits).
I want to keep these notes in a folder called _notes, just like we have _posts in source folder of Octopress.
I have a folder called _writeups/ with files such as:
subject1.html
subject2.html
I have a file called notes/list.html with following content.
---
layout: page
navbar: Notes
title: Notes
footer: false
---
<div id="blog-archives">
{% for post in site.writeups reverse %}
{% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
{% unless year == this_year %}
{% assign year = this_year %}
<h2>{{ year }}</h2>
{% endunless %}
<article class="page-header">
{% include archive_post.html %}
</article>
{% endfor %}
</div>
Basically I want to create a listing of these writeups so that I can keep updating them as and when I get time. Also I want to keep these separate from posts and pages.
How can I achieve this functionality using Octopress / Jekyll ?
Jekyll version 2, which has only recently been released, has the ability to have extra collections and data. Collections is probably what you are looking for, so upgrade your version of Jekyll and visit http://jekyllrb.com/docs/collections/ to find out more about them.

Iterate over subcategories in Jekyll

In Jekyll, a post can have multiple categories. For example, a recipe for spaghetti might have the categories dinner and food. Is it possible--without plugins--to iterate over the other categories (different dinner times) of a category (food)? For example, I want to generate the following page for the category food:
Dinner:
* Spaguetti
* Meatloaf
Breakfast
* Cheerios
* Oatmeal
Lunch
* BLT
Like a lot of Jekyll work, the answer is to loop through stuff way more than you think you should :)
Just loop through all categories or tags or whatever for your entire site, and use an if tag to avoid outputting ones that this post doesn't have.
Then inside the loop body, loop through every post on your site, using if tags again to avoid outputting the ones that don't have the category.
Here's some code that will do it for tags, I think if you replace tags with categories it'll work the same. I've lightly modified it from my own site, sorry if there's a typo or two:
{% for topic in site.tags | sort_by:topic order:ascending %}
{% if topic == whatever_topic_you_have %}
<section class="topic">
<h1><a name="{{ topic }}">{{ topic }}</a></h1>
{% for item in site.posts %}
{% if item.tags contains topic %}
...show your post/item here...
{% endif %}
{% endfor %}
</section>
{% endif %}
{% endfor %}

In Jekyll, site.posts returns nothing

I blog with Jekyll. In my source/index.html (I reconfigured the paths in _config.yml) I have written:
{{ site.posts }}
But when I compile it it gives no results. I am sure that I have posts, they are compiled and work as supposed.
I don't know where to start troubleshooting, have anyone else had such problem?
site.post returns and array of liquified Jekyll::Post objects. You can check the number of posts by simply writing:
{{ site.posts.size }}
and you could iterate through them writing:
<ul>
{% for post in site.posts %}
<li class="post">
<h1>{{ post.title }}</h1>
</li>
{% endfor %}
</ul>
In Jekyll posts need to be in the _posts folder to be included in the {{ site.posts }} variable.
There's a good chance that you may have simply omitted the underscore in your folder name.
Verify the post time, that it occurs in the past, future posts are not added to the site.posts.

An easy way to support tags in a jekyll blog

I am using the standard jekyll installation to maintain a blog, everything is going fine. Except I would really like to tag my posts.
I can tag a post using the YAML front matter, but how do I generate pages for each tag that can will list all posts for a tag?
Here is a solution with alphabetically sorted tags on a single page.
It uses Liquid only, which means that it works on GitHub Pages:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
<h3 id="{{ tag }}">{{ tag }}</h3>
<ul>
{% for post in site.tags[tag] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
You can see it in action here.
EDIT:
There's also a way to generate a separate page for each tag without plugins (which will work on GitHub Pages).
I have a more detailed explanation on my blog:
Separate pages per tag/category with Jekyll (without plugins)
First, you need a new layout file:
/_layouts/tagpage.html:
---
layout: default
---
<h1>{{ page.tag }}</h1>
<ul>
{% for post in site.tags[page.tag] %}
<li>
{{ post.date | date: "%B %d, %Y" }}: {{ post.title }}
</li>
{% endfor %}
</ul>
With this layout file, you can add a new tag page by adding a new file with just two lines of YAML front-matter.
Here's an example for the jekyll tag:
/tags/jekyll/index.html:
---
layout: tagpage
tag: jekyll
---
The only disadvantage of this approach: each time you use a new tag for the first time, you have to remember to create a new two-line file for it.
To generate the root index file (i.e. the list of tags that links to /tags/jekyll/index.html etc.), you can use a similar solution like the one on top of this answer where I generate a single page with alphebetically sorted tags:
{% capture tags %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
{{ tag }}<br>
{% endfor %}
This will generate a list of links like this:
<ul>
<li>.net</li>
<li>authentication</li>
<li>backup</li>
</ul>
Note that this solution uses a blank to split tags, so it doesn't work when your tags contain blanks and Yevgeniy Brikman's comment applies here as well.
This gist will generate a page per category for you: https://gist.github.com/524748
It uses a Jekyll Generator plugin, plus a Page subclass.
Have a look at sites using jekyll. There are a few custom forks which have implemented tagging functionality, hopefully also in the way you want :-)
I had the same question, and stumbled upon this: http://gist.github.com/143571.
It's a rake task which generates a tag list. I modified it slightly, and my version is at:
http://github.com/mattfoster/mattfoster.github.com/blob/master/Rakefile.
Whilst this doesn't give you a page per tag, you can use anchors, which is half way there!
I use the great Jekyll Tagging plugin that automatically generates a tags cloud and tag pages. Easy to install and use.
Here is a page for the "photo" tag on my blog (in french), and you can see the tags cloud in the bottom.
Based on Christian's answer above I made a bash script that does what he described.
https://github.com/ObjectiveTruth/objectivetruth.github.io/blob/master/rebuild_tags.sh
Be sure to have the accompanying 14 line vim script in the /non_website_resources/ directory
AND
Make the /_layouts/tagpage.html shown in Christian's answer above but rename it to /_layouts/tag_pages.html
File structure should be like this:
.jekyll_website_root
├── _posts
├── _layout
│ ├── tag_pages.html
├── rebuild_tags.sh
Run from the root directory ./rebuild_tags.sh
If you get permission denied error be sure to run chmod 777 rebuild_tags.sh
If you look at scripts comments its fairly simple:
Uses sed to find all the tags in every .md file in _post directory
Uses sed to massage the data to proper format
Takes all the unique tags and makes a directory and a index.html for each
This way, if you have any new tags, just run the script to rebuild the pages before pushing to github
A nice simple non-plugin way to do tags
EDIT
Removed dependency on other files. Just need the one script!
I do these with CSS. First lists an element and use the tag name as its id.
<span id="{{ site.posts | map: 'tags' | uniq | join: '"></span><span id="' }}"></span>
And then lists all the post and use its tags as a value for the "tags" custom attribute.
{% for post in site.posts %}
<article class="post" tags="{% for tag in post.tags %}{{tag}}{% if forloop.last == false %}{{" "}}{% endif %}{% endfor %}">
<h3>{{post.title}}</h3>
</article>
{% endfor %}
And then in CSS, hide all the posts by default, and only show posts with tags matches the url id/ hash
.post {
display: none;
}
{% for tag in site.tags %}#{{tag[0]}}:target ~ [tags~={{tag[0]}}]{% if forloop.last == false %}, {% endif %}{% endfor %} {
display: block;
}
/*
The compiled version will look like this
#tagname:target ~ [tags~="tagname"], #tagname2:target ~ [tags~="tagname2"] {
display: block;
}
*/
I made an article about this here.

Resources