Show a collection of values in jekyll - jekyll

Is it possible to have a collection of values in jekyll and then format them as table?
I tried something like this in my .md file:
---
layout: tutorial
title: Jekyll
reqs:
- name: names here
desc: description here
value: value
- name: names here
desc: description here
value: value
---
In my tutorial layout I have this:
---
layout: home
---
{% for item in page.reqs %}
{{ item.name }}
{{ item.desc }}
{{ item.value }}
{% endfor %}
The html code for the table was removed. The problem is my for loop prints nothing. The page is empty except for what was inherited from the other layout.

Looks like a YAML problem. You're mixing up the array and dictionary syntaxes. Try something like this instead:
---
layout: tutorial
title: Jekyll
reqs:
- item1:
name: names here
desc: description here
value: value
- item2:
name: names here
desc: description here
value: value
---
Now your for loop is looping through an array of dictionaries (item1, item2...) whose values you can use in your output.

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

How to get contenttype options in Bolt CMS

I have contenttype for products:
products_de:
name: Products (de)
singular_name: Product (de)
slug: produkte
singular_slug: produkt
tablename: products_de
fields:
...
I've set slug for german site version links. But how I can get contenttype slug from my template? I need it automaticly, because I want to add contenttype products_en with slug products for english site version.
For example, in listing template I can do
{% setcontent products = 'products_de' %}
But its get me contenttype records but not contenttype options. There is 'contenttype' key in array record, but there is may not be any products, so I'll can't get it.
Is there any global methot to get contenttype options, something like config:
{{ config.get('general/sitename') }}
UPD: I am interested in the same for taxonomies - how to get taxonomy options such as name, slug?
I found a solution. Contenttype data can be obtained with:
{{ config.get('contenttypes/pages') }}
Taxonomy data can be obtained with:
{{ config.get('taxonomy/categories') }}
So you can do just:
{% set products_data = config.get('contenttypes/products_de') %}
{{ products_data.name }}
{{ products_data.slug }}

In Jekyll, how do I render custom metadata for a collection?

I have the following in my _config.yml file:
collections:
nr_qa:
output: true
permalink: /:collection/:name
title: 'Node-RED Questions and Answers'
descriptions: 'Node-RED is a flow-based (visual) programming tool. These pages have some information that may be currently missing from the documentaiton.'
github_pages:
title: 'GitHub Pages and Jekyll/Liquid'
description: 'Hints and tips on using Jekyll for publishing to GitHub Pages.'
output: true
permalink: /:collection/:name
and I want to create an automatic index for my collections. So I use code like this:
## {{ site.collections.github_pages.title }}
{{ site.collections.github_pages.description }}
<ul>
{% for item in site.github_pages %}
<li>
{{ item.title | replace:'_',' ' }}
<p>{% if item.description %}
{{ item.description }}
{% else %}
{{ item.excerpt | strip_html }}
{% endif %}</p>
</li>
{% endfor %}
</ul>
And yes, I know I've rather mixed up my markdown and html there. Not relevant to this question.
The problem is that {{ site.collections.github_pages.title }} and {{ site.collections.github_pages.description }} don't render anything even though I think they should.
Can anyone point out my mistake please?
The problem is that title and description should be included in each collection, and not in _config.yml.
Check out Accessing Collection AttributesPermalink for further details.
update
title can be present in each collection metadata in _config.yml. The problem is how you are accessing those variables.
One approach is to have a specific layout for each collection, then you can access them like:
{% assign col = site.collections | where: 'label','github_pages' | first%}.
TITLE: {{ col.title }}.
DESCRIPTION: {{ col.description }}.

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.

How to do CSV lookups in ansible where the key comes from a variable?

So ansible has the possibility of looking up things from a CSV file, the example on their web page is:
- debug: msg="The atomic number of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=,') }}"
- debug: msg="The atomic mass of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=, col=2') }}"
Now, my CSV file contains a mapping of hostnames to a number, like this:
HOST,ID
foo,0
bar,1
Now, when I adapt this to:
- debug: msg="My ID is {{ lookup('csvfile', '{{ inventory_hostname }} file=my.csv delimiter=,') }}"
I get the error:
Failed to template msg="My ID is {{ lookup('csvfile', '{{ inventory_hostname }} file=my.csv delimiter=,') }}": need more than 1 value to unpack
How do I do this right?
use the string formatting
- debug: msg="My ID is {{ lookup('csvfile', '{} file=my.csv delimiter=,'.format(inventory_hostname)) }}"