Liquid Template: Append variable to data object - jekyll

I have the following structure in my Jekyll app:
_data/
test.json
items/
test/
index.html
I am using the following to grab just the name of the ending folder name of my item:
{% assign listing = {{ page.url | remove: 'items/' | replace:'/',' ' | truncatewords: 1 | remove:'...' | escape }} %}
What I'm then trying to do is access the data file which the matching folder name from the _data directory.
I've gotten it to:
{{ site.data.{{ listing }} }}
which allows me to see the data, but I can't actually go inside the JSON object to grab a specific item, like {{ site.data.{{ listing }}.test }} does not work. Any help would be greatly appreciated. Thanks!

Use some brackets like this :
{% assign datas = site.data[{{listing}}] %}
You can now access datas.test.

Related

jinja2: Exclude or reject items from template on include

Thanks in advance to anyone looking at this. I'm new to jinja and am trying to test something with the following 3 simple files:
pipe.j2:
{% include 'resources.yml' %}
resources.yml:
resources:
- name: resource1
type: aThing
- name: resource2
type: anotherThing
test.sh
j2 pipe.j2 > pipeline.yml
The test.sh script runs fine, but i'm trying to exclude either of the resources in the resources.yml file. I've tried the following in the j2 file getting different errors:
example1:
{% include 'resources.yml' | reject('equalto','resource1') %}
TypeError: generator is not subscriptable
example2:
{% include 'resources.yml' %}
{% set r_list = 'resources' %}
{% for rsc in r_list | rejectattr('name', 'equalto', 'resource1') %}
{% endfor %}
UndefinedError: 'str.object' has no attribute 'name'
I checked online and haven't found any examples of what I'm trying to accomplish and am not sure if it's possible or if I'm doing it completely wrong. I'm trying to get a file that only contains non-rejected items as a final result.
Hoping to get in pipeline.yml file:
resources:
- name: resource2
type: anotherThing

jekyll find filename portion of page url

I need to extract the filename portion of the page url, that is from a post saved in 2011-12-31-new-years-eve-is-awesome.md I would like to just the part new-years-eve-is-awesome .
Unfortunately post_url contains also the directory tree 2011/12/31/
This page https://jekyllrb.com/docs/permalinks/ seems to suggest that defining
---
shorttitle: :title
---
in the front matter should work but that produces an empty string from {{ post.shorttitle }}
Here is my solution so far. Perhaps there is a variable left in the code but it's not documented, therefore I just filter the post URL:
{% assign spliturl = post.url | split: '/' %}
{% assign postname = spliturl[4] | replace: '.html', '' %}
{{ postname }}

Is there a way to dynamically get a list of documents in a Jekyll collection

I'm fairly new to Jekyll, and am currently trying to build a site to hold a bunch of pages containing course training material. I'm trying to use Jeykll collections to do this and have a site structure like this:
/
|
|_ "_course001"
|
|_ subdirs/subdocs
|_ "_course002"
|
|_ subdirs/subdocs
When rendering content for each course (setup as a collection) I would like a navbar to display the other documents in the current course/collection only (i.e. not in other courses).
Using something like this works:
{% for each coursedoc in site.course001 %}
[Create anchors and text]
{% endfor %}
However I want to have the collection name by dynamically assigned during the site build rather than hardcoded. I've tried doing something like this:
{% for each coursedoc in {{ page.collection | prepend: "site." }} %}
[Create anchors and text]
{% endfor %}
But this does not work, I guess because {{ page.collection | prepend: "site." }} returns a string.
Anyone have some suggestions on how I could do this?
And I think I just answered my own question:
{% assign collection = site.collections | where: "label", page.collection | first %}
{% for node in collection.docs %}
[Create anchors and text]
{% endfor %}

How to pass variables from layout to page in Liquid?

I try to create a global variable in layout and call it in pages
E.g:
<!-- _layouts/post.html -->
{% assign filename = page.url | split: '/' | last | replace: '.html', '' %}
{{ content }}
in post page:
<!-- SomePage.html -->
<h1>Page Name: {{ filename }}</h1> //-> # Page Name: SomePage
The page name should be printed but I do not know how to do it
Any ideas?
I guess you are misusing Jekyll as it is not suppose to work like that. It should be used other way around. Templates are designed to show pages with variables, so that you follow many to one relation pages in templates.
The idea is to put repetitive part of your pages in your layout. So you should have your layout defined like this:
<!-- _layouts/post.html -->
{% assign filename = page.url | split: '/' | last | replace: '.html', '' %}
<h1>{{ filename }}</h1>
{{ content }}
Then you don't have to write that part in your every page, but it is handled automatically for you.

Jekyll site.categories incorrect values

When I access site.categories.first I get what looks to be all the content of all my blog posts wrapped into a single string.
When I access site.categories[1] I get an empty string. The length of site.categories appears roughly equal to the number of categories I have.
I checked for any manual editing of site.categories, but I don't see anything that would be doing this.
You can use inspect filter to understand how categories works.
{{ site.categories | inspect }} returns a hash like:
{
"jekyll"=>[#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>],
"update"=>[#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>]
}
And {{ site.categories.first | inspect }} returns an array like :
["jekyll", [#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>]]
Where {{ site.categories.first[0] }} is the category name, and {{ site.categories.first[1] }} is an array containing first category's document.
You can call a category from his name {{ site.categories.jekyll | inspect }} but not by is index {{ site.categories[0] | inspect }} => []
You cannot modify site.categories because it is freezed by jekyll.