Struggling to find array in Jekyll config file (site navigation) - yaml

In my _config.yml I have:
navigation:
- text: Home
url: index.html
- text: About
url: about.html
- text: Blog
url: blog.html
- text: Portfolio
url: portfolio.html
In my default.html layout I have:
<ul class="nav-list">
{% for link in site.navigation %}
{% assign current = nil %}
{% if page.url == link.url %}
{% assign current = 'current' %}
{% endif %}
<li class="nav-list-item{% if forloop.first %}first{% endif %} {{ current }} {% if forloop.last %}nav-list-item-last{% endif %}">
<a class="{{ current }}" href="{{ link.url }}">{{ link.text }}</a>
</li>
{% endfor %}
</ul>
For some reason, this is not working. Why might this be?

I just found out you have to restart the server and run jekyll --server again and the _config.yml variables will be accessible.
Then after figuring it out came back to this page and was about to post this answer, clicked add / show 4 more comments and saw that this was also found out by the op. So posting it as answer.

In addition to the other answer: You can - as you mentioned in your answer - use Ctrl + C to end the currently running server (in your CLI). But, you can as well start the server with
jekyll server -w
to tell Jekyll to watch for changes in your files. Whilst this won't work for changes to the _config.yml, it works for all other files. Note, that this won't work including the --safe option, so you won't get errors. To catch them, I still recommend to run jekyll --safe build from time to time to see if you got syntax errors. The same goes for running jekyll doctor/jekyll hyde sometimes during your dev process.

Related

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.

Jekyll Pagination Path Doesn't Include Page Number

I am attempting to create pagination in Jekyll under the path '/notes/'.
In my _config.yml, I have:
paginate: 2
paginate_path: "/notes/:num/"
In my /notes/index.html, I have:
<div class="pagination">
{% if paginator.previous_page %}
Previous
{% else %}
<span class="previous section link">Previous</span>
{% endif %}
<span class="page_number section">{{ paginator.page }} / {{ paginator.total_pages }}</span>
{% if paginator.next_page %}
Next
{% else %}
<span class="next section link">Next</span>
{% endif %}
</div>
When I load http://localhost:4000/notes, I see that the 'Previous' link is not linking to /notes/2, but instead it simply links to /notes/, the page that I am already on. Additionally, when looking in the generated _site folder, I see that /notes/2 has not been created, despite having more than two posts created.
So, I suppose my question is, how can I fix this and get jekyll to paginate properly?

Create categories in Jekyll _data?

I need help using _data in Jekyll to generate content.
Let's say I want to create a directory displaying countries that have blocked Twitter and Facebook. I understand how to query the _data folder but how do I create something like categories in the _data .yml and query that data?
Let me try to explain.
Twitter is blocked in Turkey & Iran so I'd start with this (networks.yml in _data):
- network: Twitter
This is where I get stuck. What I don't understand is if I'd want to "tag" or "categorize" Twitter like this:
- network: Twitter
blockedcountries: [Turkey, Iran, Iraq]
- network: Facebook
blockedcountries: [Iraq, Turkey]
Then I'd want pages at mysite.com/turkey/ that would display the networks tagged with turkey. Something like this:
{% for network in site.data.networks %}
{% if network.blockedcountries == Turkey %}
<li>{{ network.network }} is blocked in Turkey</li>
{% endif %}
{% endfor %}`
Which would display:
- Twitter is blocked in Turkey
- Facebook is blocked in Turkey
Appreciate any help and will tip Bitcoin if solved!
Your YAML is right. The Problem seems to be in your if statement:
{% if network.blockedcountries == Turkey %}
network.blockedcountries is an array(a list) and therefore your if statement needs to be like this:
{% if network.blockedcountries contains "Turkey" %}
<li>{{ network.network }} is blocked in Turkey</li>
{% endif %}
Jekyll is using the Liquid markup language for its templates. You can read more about its possibilities here. Maybe the liquid case statement is also helpful for further optimization.
Here is my "full" solution:
My data in _data/networks.yml
- name: Twitter
blockedcountries: [Turkey, Iraq, Iran]
- name: Facebook
blockedcountries: [Turkey, Iraq]
My liquid template in index.html
<ul>
{% for network in site.data.networks %}
{% if network.blockedcountries contains "Turkey" %}
<li>{{ network.name }} is blocked in Turkey!</li>
{% endif %}
{% endfor %}
</ul>

octopress: how to display HTML based on user path

TLDR - How do I access the current user path in octopress/jekyll?
On my Octopress blog, I would like to display an HTML element only when the user is on the root path. The trouble is that {{page.url}} returns /index.html on the root path, while my root path in _config.yml is set to '/'.
Thus, this conditional does not work:
{% if page.url == site.root %}
<div class="blurb">
<p>{{ site.description }}</p>
</div>
{% endif %}
When I change the root in _config.yml to match /index.html it breaks all of the CSS. Why is page.url pointing to index.html? There is no /index.html in the url of my live website. Is /index.html referencing a controller somewhere?
Is there an easy way to access the current user path in Octopress/Jekyll?
For reference - I am pulling the page.url variable from a Jekyll doc. {{site.root}} refers to the root value in the _config.yml file.
Thanks!
I solved this by hardcoding '/index.html' into the conditional.
{% if page.url == 'index.html' %}
<div class="blurb">
<p>{{ site.description }}</p>
</div>
{% endif %}
In source/_layouts/default.html
{% if page.front_page %} {% include front_page.html %}{% endif %}
and then add front_page: true
in index.html
---
layout: default
navbar: Blog
front_page: true
---

Post without title in Jekyll

In my Jekyll blog, I would like some posts not to have a title. How could I modify the Jekyll codebase to make it so that posts do not require a title?
You don't need to alter the jekyll codebase to remove titles. That can be done using different layouts with appropriate liquid filters and tags.
For individual post pages, simply make a new layout file (e.g. "_layouts/no-title-post.html") that doesn't have the {{ page.title }} liquid tag. In your _posts source file, set the YAML front matter to call it. For example:
---
layout: no-title-post
---
Note here that "title:" isn't required in the YAML front matter. If jekyll needs it, the value will be automatically crated from the filename. For example, "_posts/2012-04-29-a-new-post.md" would have its title variable set to "A New Post" automatically. If your templates don't call the title tags, it won't matter. You could include a "title:" in the front matter and it simply wouldn't be displayed.
You can also display the page without the title in your listing/index pages. Check the posts layout to determine if the title should be displayed. For example, to show titles on all your pages except ones that have the 'no-title-post' layout, you would do something like this:
{% for post in paginator.posts %}
{% if post.layout != 'no-title-post' %}
<h1>{{ post.title }}</h1>
{% endif %}
<div class="postContent">
{{ post.content }}
</div>
{% endfor %}
In that case, the link to the page itself is also removed. If the page needs to be addressable, you would have to add the link back in somewhere else.
edemundo's solution doesn't work anymore in all cases with Jekyll 3.
I use an empty title as default:
defaults:
-
scope:
type: "posts"
values:
layout: "post"
title: ""
Then you can compare titles against the empty string in your layouts, for example:
{% if post.title == "" %}
{{ post.content | strip_html | truncatewords:5 }}
{% else %}
{{ post.title }}
{% endif %}
If you like the automatic title generation you can use as frontmatter:
---
title: ""
---
I was having the same doubt, then I stumbled on this really simple solution:
{% if post.title %}
<h1>{{ post.title }}</h1>
{% endif %}
And then in the post file itself you would leave the title variable empty:
---
layout: post
title:
---
This way, the h1 will not print if the title is empty. I found this method particularly useful for post types like quotes, which most of the time doesn't have titles.

Resources