Get data from data folder? - jekyll

I need to get some data using a dynamic name from front matter:
{{ site.data.prd-[page.tag].title" }}
The above fails to get the string from the data folder and nothing is output.
Where am I going wrong?

I am not sure about what you are trying to do but how about make a dictionary type data in _data directory and search for a title that matches page.tag in desired pages?
_data.prd-tags.yml
For each item, we can access the key and value using index ([0]: key, [1]: value).
jekyll: "jekyll's title"
ruby: "ruby's title"
html: "html's title"
.
.
.
HTML fragment for searching and displaying a tag (if found)
Iterate over site.data.prd-tags and display title if an item that matches page.tag is found.
<div class="tag-title">
{% for tag in site.data.prd-tags %}
{% if tag[0] == page.tag %}
{{ tag[1] }}
{% else %}
Nothing found
{% endif %}
{% endfor %}
</div>

Related

jinja2 - create href with variable from loop

I'm try to create a dynamic href for a website
I've tried this:
(where "gruppe" is a list of servers)
{%- for item in groups[gruppe] %}
{% set url = 'https://cmk.abc.local/abc/check_mk/view.py?host=' + {{ hostvars[item]['openstack']['name'] }} + '&site=abc&view_name=host' %}
{{ hostvars[item]['openstack']['name'] }}.abc.local
{% endfor %}
Expected result should be:
https://cmk.abc.local/abc/check_mk/view.py?host=server01&site=abc&view_name=host#
Does anyone have an idea what I'm doing wrong?
It's tough to say without knowing the shape of the data. If you can post what "groups" looks like (as JSON) that would be helpful.
The first thing that stands out to me is "gruppe". Is that supposed to be a key in the groups object or is that supposed to be dynamic?
Try:
{%- for item in groups["gruppe"] %}
...

How do I access a data value in a dictionary in the html template

I'm passing a dictionary in the context in my views.py to my html template. How do I know access a value in the template based on a particular key. For instance I'd wanna do something like {{ dictionary.keyname.value }} but I don't know the correct syntax and for some reason I can't seem to find the documentation.
I want to achieve the same effect as this without having to use a for loop:
<b>Calories</b>
{% for key, value in output.items %}
{% if key == "calories" %}
{{ value }}
{% endif %}
{% endfor %}
You just want {{ output.calories }}.

Jinja "==" (compare condition) doesn't work?

while writing an application in django, I've encountered a problem. I want to make page-number links, with current page not being a link. So in template I do this:
{% for i in pages %}
{% if i == curr_page %} {{ i }}
{% else %} {{ i }}
{% endif %}
Only problem? Jinja doesn't seem to notice two numbers being equal. I've changed the 2nd line to {% if i != curr_page %} {{i}}!={{curr_page}} and got "... 5!=6 6!=6 7!=6 ...".
What should I do?
Because they are not of same data type. In your view, cast them to int before passing to context dict.
pages = list(map(int, pages))
curr_page = int(curr_page)

Check markdown file has value with Jekyll template

I have a loop that goes through a folder of markdown files and displays the titles of each in a dropdown. I need to be able to filter the results of the loop based on whether or not a markdown file has an "office" value in it.
I currently have this:
<select name="practice-area" type="search" class="practice-areas-list select-table-filter" data-table="order-table">
<option value="default">Practice Areas</option>
{% for practice_area in site.practice_areas %}
{% unless practice_area.office %}
<option value="{{ practice_area.title }}">{{ practice_area.title }}</option>
{% endunless %}
{% endfor %}
<select>
where {% unless practice_area.office %} should be checking if the file has office in it. If so, pull title into list.
Sample Markdown File
---
title: page title
slug: page-title
office:
-22
---
Not sure of the proper Jekyll syntax for this to work correctly.
if tag contains a section of template which will only be run if the condition is true while unless would be run if the condition is false.
So to check if that key is present in your post front-matter, then you need to use the if statement:
{% if practice_area.office %}
...
{% endif %}
Realized I needed the opposite values so this works:
{% unless practice_area.office %}
{% endunless %}
Gives me all that don't have an office.

How to pass a frontmatter value into a for loop

I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.
I have a data file in _data/sidebars/sidebar1.yml. It contains:
- first
- second
- third
On a page I have this frontmatter:
---
title: My page
sidebar: site.data.sidebar.sidebar1
---
I want to use this code:
{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}
However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).
The only thing that works is to do this:
{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}
{% for entry in sidebar %}
* {{entry}}
{% endfor %}
However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.
Any ideas on how to make this work?
You have a typo in your front matter. It's :
sidebar: site.data.sidebars.sidebar1
not
sidebar: site.data.sidebar.sidebar1
You can even be less verbose.
In your front matter : sidebar: sidebar1
In your code :
{% assign sidebar = site.data.sidebars[page.sidebar] %}
{% for entry in sidebar %}
{{ entry | inspect }}
{% endfor %}