Jekyll blog - How to show a list of posts if list file exists - jekyll

What I'm trying to do is for each category page, if a file exists with the category name then use that file for the list of popular posts. If not then I want to use the default popular posts.
The category featured post file would exist in this folder, if there is one "_includes/blog/feat-posts."
I want to display in the sidebar of my blog. Here's the code I tried using some documentation I found online. This does not work.
<h3>POPULAR POSTS</h3>
{% comment %}(Not sure if this is the correct code, but looking for static file){% endcomment %}
{% for static_file in site.static_files %}
{% comment %}(if a file exists with the same name as the category name, then..){% endcomment %}
{% if static_file.path == '_includes/blog/feat-posts/{{page.autopages.display_name}}.html' %}
{% comment %}(include that file){% endcomment %}
{% include {{ blog/feat-posts/{{page.autopages.display_name}}.html }} %}
{% else %}
{% comment %}(otherwise use the default files){% endcomment %}
<p>Filename1 Text</p>
<p>Filename2 Text</p>
{% endif %}
{% endfor %}
Please help.

I still don't know what "it does not work" means but I assume the error happens in the line with include or your file is not in your static_sites.
You can check the site.static_files content by adding {{ site.static_files | inspect }} to your code. Static files are kind of special, see https://jekyllrb.com/docs/static-files/.
To construct paths dynamically, you may want to use assign to assign the value to a variable first (see simplified example below).
I could check the path by adding a file to the root folder (path /test.html) and then link to the same file name in the _includes folder:
<h3>POPULAR POSTS</h3>
{% comment %}{{ site.static_files | inspect }}{% endcomment %}
{% for static_file in site.static_files %}
{% if static_file.path == '/test.html' %}
{% assign filename = 'test.html' %}
{% include {{ filename }} %}
{% endif %}
{% endfor %}
I hope this helps.

Related

Jekyll/Liquid Relative URL filter breaks links

I'm trying to use relative_url in most of the links of my Jekyll theme, so if someone wants to have this theme working in a subdirectory he can do it.
I have a problem with the list of categories of the post, each of which should link to the archive.
In _layouts/post.html I have this code:
{% if site.data.settings.categories.active %}
{% include categories.html %}
{% endif %}
categories.html has this code:
<div class="categories">
<span><p>Categories:</p>
{% if post %}
{% assign categories = post.categories %}
{% else %}
{% assign categories = page.categories %}
{% endif %}
{% for category in categories %}
{{category}}
{% unless forloop.last %} {% endunless %}
{% endfor %}
</span>
</div>
Here's the problem:
{{category}}
Somehow, this returns the current post url.
{{category}}
This returns the correct link, but does not work in case the site is in a subdirectory.
Why it returns the post url?
There are multiple problems here.
First off, Liquid doesn't evaluate nested constructs.
Therefore, the following code:
{{ "/categories/#{{category | slugify}}" | relative_url}}
needs to be rewritten into:
{% capture url %}/categories/{{ category | slugify }}{% endcapture %}
{{ url | relative_url }}
Secondly, there is no global post object. Therefore {% if post %} is always going to evaluate to a negative. i.e., it is redundant.

Cannot load multiple commenting system in Jekyll blog

I am trying to implement disqus and facebook commenting system for different categories in jekyll blog.
Here is my current approach.
{% for category in site.categories %}
{% if category.type == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Expected result: Facebook comment should be loaded in category personal from facebook.html otherwise disqus comment should be loaded in all other category.
Actual result: Disqus comments loads automatically in personal category regardless of the loop.
What should be changed to make the comments load correctly?
It seems that you want to print disqus or facebook on each page depending on "personnal" category presence.
As #marcanuy says, you have to refer to page.categories, which is an array.
{% if page.categories contains "personnal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
Category doesn't have a type attribute. Checking directly like:
{% for category in page.categories %}
{% if category == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Should detect the personal category and load disqus comments.

How to include all files in a folder in Jekyll?

In Jekyll you can include a file by doing:
{% include some-folder/some-file.html %}
If you add more files to the folder you have to manually add them:
{% include some-folder/some-file-2.html %}
{% include some-folder/some-file-3.html %}
{% include some-folder/some-file-4.html %}
Is there a way in Jekyll to include all files in a folder automatically?
It seems possible to have a variable name in an include (source).
If this is allowed:
{% include {{ page.my_variable }} %}
Then also this is allowed:
{% include include_all_files.html folder="some-folder" %}
With an include_all_files.html that looks like this:
{% for file in site.static_files %}
{% if file.path contains include.folder %}
{% include {{ file.path }} %}
{% endif %}
{% endif %}
I have found a way to include the content of all files in the current folder not including the current file.
{% assign parent_path = page.path | split:'/' | last %}
{% assign parent_path = page.path | remove: parent_path %}
{% for file in site.static_files %}
{% if file.path contains parent_path %}
{% assign file_name = file.path | remove: parent_path | remove: "/" %}
{% include_relative {{ file_name }} %}
{% endif %}
{% endfor %}
Reason for my setup: I have a large markdown file (main.md) that contains a lot of information, so I needed a way to move the content into separate files and included them with main.md.

Find post where key has specific value in Jekyll

I need to be able to compare a value in each of the markdown files in a folder called in_media to the user_id of the current page stored in a _users folder and only display the post title that have that value from the in_media folder.
User markdown file in _users folder
---
user_id: 123
title: bob
---
Post markdown from in_media folder
---
users: 123
---
I tried the following:
{% for this_user in site.in_media %}
{% for user in page.user %}
{% if this_user == user.user_id %}
<li><a href="{{ post.external_link }}">{{ post.title }}</a </li>
{% endif %}
{% endfor %}
{% endfor %}
However, this is not returning anything
Try this way:
{% for post in site.in_media %}
{% if post.value == page.title %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
I'm not sure that you can use external_link, never heard of it. Maybe you'll need to build permalink manually - depending on your _config.xml.
Also note the collection should be properly set up to work with permalinks.

Include a file, but only if it exists?

I'm creating a style guide in Jekyll and using Collections to define different elements of the guide. For example, headings, lists, etc.
I'm trying to separate the Sass into files that match up with the partials, one to one, and I'd like to render the Sass files as part of each collection.
So, something like:
{% if _includes/_sass/{{ entry.title | append: ".scss"}} %}
{% highlight sass %}
{% include _includes/_sass/{{ entry.title | append: ".scss" }} %}
{% endhighlight %}
{% endif %}
Basically, what I want is "Include a file in this directory that has the same name as this entry in my collection. If it doesn't exist, don't break."
How do I do this? I've explored storing the file path in a variable but can't seem to get that to work.
Thanks in advance.
It can be done.
This works on Jekyll 3 but it can certainly be ported to Jekyll 2.
Starting from a base install (jekyll new)
_config.yml
collections:
guide:
sasssamples:
Style guide files
Our samples will be grouped in the _guide collection.
Example file : _guide/header/header1.hmtl
---
title: Header level 1
---
<h1>Header level 1</h1>
SCSS samples
We want our SCSS samples to be included in our css/main.scss and use variables defined in our other SCSS files. Our samples will be integrated at the end of our css/main.scss
We don't want our SCSS samples to render as css so no .scss extension. Switch to .txt extension
We want to access SCSS samples from a list. Let's put them in a sasssamples collection.
Example file : _sasssamples/header/header1.txt
---
---
h1{
color: $brand-color;
border: 1px solid $brand-color;
}
SCSS samples integration
Add this code at the very end of you bootstraping scss file (css/main.scss on a base Jekyll install)
css/main.scss
[ original code ... ]
{% comment %} Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
{% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
{% comment %}
Printing documents in sasssamples collection.
All SCSS from style guide are sandboxed in .guide class
This allows us to apply styles only to style guide html samples
{% endcomment %}
.guide{
{% for doc in scssCollection.docs %}
{{ doc.content }}
{% endfor %}
}
The style guide
<h2>Style guide</h2>
{% comment %}Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
{% assign guideCollection = site.collections | where: 'label', 'guide' | first %}
{% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
{% comment %} Looping hover style guide samples {% endcomment %}
{% assign samples = guideCollection.docs %}
{% for sample in samples %}
<article>
<h3>Element : {{ sample.title }}</h3>
<h4>Render</h4>
<div class="guide">
{{ sample.content }}
</div>
<h4>html code</h4>
{% highlight html %}{{ sample.content }}{% endhighlight %}
{% comment %}
Changing a path like : _guide/headers/header1.html
to : _sasssamples/headers/header1.txt
{% endcomment %}
{% assign scssPath = sample.path | replace: '_guide', '_sasssamples' %}
{% assign scssPath = scssPath | replace: '.html', '.txt' %}
{% comment %} Try to find a SCSS sample with equivalent path {% endcomment %}
{% assign scssSample = scssCollection.docs | where: 'path', scssPath | first %}
{% comment %}We print SCSS sample only if we found an equivalent path{% endcomment %}
{% if scssSample != nil %}
<h4>SCSS code</h4>
{% highlight css %}{{ scssSample.content }}{% endhighlight %}
{% endif %}
</article>
{% endfor %}
Done!
Seems it only miss on assigning the correct path
{% if _includes/_sass/{{ entry.title | append: ".scss"}}
Need to be replaced to relative path to the scss file:
{% assign scssPath = 'relative/path/to/your/scss/' %}
{% if {{ entry.title | append: ".scss" | prepend: scssPath }} != nil %}