On my personal site, I style the post list on my home page based on the post's category. So if I have the front matter:
---
layout: post
title: "My Post"
date: 2016-01-26
category: code
---
Because the category is code, the div below will render to have a css class card-code and style the post excerpt accordingly.
{% for post in site.posts %}
<div class="col-1-2">
<!-- This Line! -->
<div class="paper-card card-{{ post.category }}">
<!-- other code -->
</div>
</div>
{% endfor %}
To try to make my life simpler, I set the default category for posts to be general in my _config.yml file:
default:
-
scope:
path: ""
type: "posts"
values:
category: "general"
However, {{ post.category }} comes up with nothing for these posts (i.e., card-). Any ideas why this ain't working?
Reading the documentation at Front Matter Defaults the section should be called defaults not default.
Try this in your _config.yaml
defaults:
-
scope:
path: ""
type: "posts"
values:
category: "general"
Related
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.
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>
First of all, I would like to say I have tried for 2+ hours to try and correct this error, but alas I cannot figure it out. I am also a newbie to Jekyll and Github Pages. However, I am competent in code and can correct any errors.
When I go to my github pages for this repository:
Repository link 2:https://github.com/jeffward01/Conscience-Alchemy.git
Github pages link: https://jeffward01.github.io/Conscience-Alchemy
There is no formating applied. I know/suspect its a problem with my base.url and/or url in the _config.yml file. I have tried 14 combinations and cannot figure it out.
When I run jekyll serve --baseurl it runs and applies the formating/styles... But, when I go to the GitHub pages, it does not...
Does anyone have any tips/advice/answers?
Thanks!!
For your ease, here is my _config.yml file:
`
# Site settings
title: Conscience Alchemy
description: "A blog about Conscienceness and Alchemy"
url: "http://jeffward01.github.io"
author:
name: "Jeff Ward"
email: "conscience-alchemy#gmail.com"
url: "http://jeffward01.github.io"
baseurl: /Conscience-Alchemy
# Build settings
baseurl: /Conscience-Alchemy
markdown: kramdown
source: .
destination: ./_site
permalink: /:title
paginate: 8
paginate_path: /page:num/
# Default values
defaults:
-
scope:
path: ""
type: "posts"
values:
layout: "post"
# Custom variables
version: "1.68"
# Options
custom_header: false
custom_nav_footer: false
reverse: false
'
And here is the beginning of my index.html file (I was not sure if you needed other file so I did not add them. They are on my GitHub Tree tho.)
---
layout: default
---
<!-- Posts -->
<ul id="posts">
{% for post in paginator.posts %}
<li class="post">
<h2>{{ post.title }}</h2>
<time datetime="{{ post.date | date_to_xmlschema }}" class="by-line">{{ post.date | date_to_string }}</time>
<p>{{ post.content | strip_html | truncatewords:50 }}</p>
</li>
{% endfor %}
</ul>
In your _config.yml repository you have baseurl: /, I think you must change it to baseurl: /Conscience-Alchemy.
The baseurl: subpath in the _config.yml file must be pointed to the project's repository name.
Example: baseurl: "/<repositoryname>" or, in this case baseurl: "/Conscience-Alchemy"
I've got a design for my post page that is made up of text and images, more specifically, galleries. Take a look at the illustration below:
I've got these galleries set up my front matter like so:
---
gallery-1:
rows:
- images:
- { url: '1.jpg'}
- images:
- { url: '2.jpg'}
- { url: '3.jpg'}
gallery-2:
rows:
- images:
- { url: '4.jpg'}
- { url: '5.jpg'}
---
Is there a way to print these galleries to the page on my .md file. I know the code below won't work, but something similar?
This is my markdown
[gallery-1]
This is more markdown
[gallery-2]
Is something like this possible with Jekyll?
Any help with this is appreciated. Thanks in advance!
First, let's simplify your yaml front matter :
---
galleries:
# gallery number one
1:
# row one in gallery one
-
- { url: '1.jpg', alt: 'alt 1'}
# row two in gallery one
-
- { url: '2.jpg', alt: 'alt 2'}
- { url: '3.jpg', alt: 'alt 3'}
# gallery number two
2:
# row one in gallery two
-
- { url: '4.jpg', alt: 'alt 4'}
- { url: '5.jpg', alt: 'alt 5'}
other front matter like title, ...
---
Your .md file :
Markdown
{% include gallery.html gallery=1 %}
Other markdown
{% include gallery.html gallery=2 %}
And then the _includes/gallery.html file :
{% comment %}-----------------
- This page receives a gallery index (include.gallery)
- It then assign the gallery[include.gallery] to the rows variable
%}-----------------{% endcomment %}
{% assign rows = page.galleries[include.gallery] %}
{% comment %}%}-----------------
We now loop over rows
%}-----------------{% endcomment %}
{% for row in rows %}
<div class="row">
{% comment %}%}-----------------
and then loop over images in row
%}-----------------{% endcomment %}
{% for img in row %}
<p>src="{{ site.baseurl }}{{ img.url }}" alt="{{ img.alt }}"</p>
{% endfor %}
</div>
{% endfor %}
See yaml documentation And Jekyll template documentation
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.