jinja2 - create href with variable from loop - html

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"] %}
...

Related

Passing list of Relation object to dbt_utils.union_relation macro fails

Related to dbt and jinja2
I am using union_relations from dbt_utils package (0.5.0).
I created my macro which takes list of fully qualified name (like database.schema.identifier) splits it and uses api.Relations.create (link) to create a relation and append each relation to a list.
{{ list_of_relation }} is given to dbt_utils.union_relations(as relations=my_macro([list of fully qualified names])), it's giving me an _is_relation error, I did use log to debug and see if it actually creates a relation and it does. What could be wrong?
It sounds like you have a macro written something like this:
{% macro my_macro(names) %}
{% set list_of_relations = [] %}
{% for name in names %}
{% set relation = something(name) %}
{% do list_of_relations.append(relation) %}
{% endfor %}
{{ list_of_relations }}
{% endmacro %}
Instead of using {{ list_of_relation }}, you’ll want {{ return(list_of_relation) }} or {% do return(list_of_relation) %}. The problem is that {{ ... }} turns things into strings in jinja macros, and macros by default return strings.
The documentation on return is here.

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)

Get data from data folder?

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>

In Jekyll, I want to loop through items in a list and set assign tags for each item

In Jekyll, I want to loop through items in a list and assign variables to each of them.
In a data file, my list looks like this:
entries:
- title: Sidebar
subcategories:
- title: Overview
items:
- title: Introduction
url: /introduction/
linkname: intro
- title: Release Notes
url: /release_notes/
linkname: relnote
My looping logic looks like this:
{% for entry in sidebar %}
{% for subcategory in entry.subcategories %}
{% for item in subcategory.items %}
{% assign item.linkname = "{{item.title}}" %}
{% endfor %}
{% endfor %}
{% endfor %}
Then I want to insert the variable using {{intro}} or {{relnote}} on a page. Using this method, I should be able to generate variables for all the items in my sidebar, making it easy to embed links in my content.
However, this method doesn't seem to work. Nothing appears for the variable when I insert it. What am I doing wrong?
With Jekyll/Liquid, you cannot create a variable name from a variable value.