I'm starting off with Jinga and can't seem to figure out how to find a string after a specific character.
Here is an example:
{{'#' in "Test1 Test2 #Test3 Test4 #Test5 #Test6" }}
I was able to get as far as testing a string if it contains the "#" character.
Now I would like to be able to somehow pull out the full words after the "#"(and before the next space) so that the resulting variable looks like this:
Test3 and Test5 and Test6
Is this possible in Jinja templates?
Here is the perfect solution:
{% for word in "Test1 Test2 #Test3 Test4 #Test5 #Test6".split() if "#" in word %}
{{ word.replace('#', '') }}
{%- endfor %}
Related
For some reason, I can't access an array within a JSON metafield.. I've tried the other StackOverflow answers, and I'm using value, etc. but just can't figure it out, here's my metafield:
product.metafields.artist.releases
with a value of:
{
"releases": [
{
"id": 0,
"releaseName": "lofi 1",
"coverArt": "",
"releaseLink": “”
},
{
"id": 1,
"releaseName": " lofi 2",
"coverArt": "",
"releaseLink": “”
}
]}
(which formats to: "{\"releases\":[{\"id\":0,\"releaseName\":\"lofi 1\",\"coverArt\":\"\",\"releaseLink\":“”},{\"id\":1,\"releaseName\":\"lofi 2\",\"coverArt\":\"google.com\",\"releaseLink\":“”}]}")
and I'm using this in the product.custom.liquid:
{{ product.metafields.artist.releases.value }}
{% assign releases = product.metafields.artist.releases.value %}
{% for release in releases.releases %}
{{ release.releaseName }}
{% endfor %}
the first one shows up fine, and if I assign it and do {{ releases }} it shows up fine as well so I know the assignment is working, but I can't forloop over it (mind you that the first object in the JSON is also called releases (I've also tried renaming it all to unique names just in case and that didn't help))
For some reason when it is a multidimensional JSON array it acts weird. There is a simple fix for it, just add (-) at the end of your assigned variable:
{%- assign releases = product.metafields.artist.releases.value -%}
{% for release in releases.releases %}
{{ release.releaseName }}
{%- endfor -%}
Hope it solves your problem like it did mine!
Liquid is not going to work on JSON like this. If you want to iterate through an array of JSON objects, use Javascript.
As lov2code points out by adding (-) it trims the output for any unnecessary white space, which enables you to traverse the JSON array.
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"] %}
...
I need to capture a string into a variable tag, then use this variable in site.tags.tag. The code is:
{% capture tag %}programming{% endcapture %}
{{ tag }}
{%- assign titles = site.tags.tag | map: "title" -%}
{{ titles }}
This code only prints prints:
programming
But if I replace site.tags.tag with either site.tags.programming or site.tags.'programming' I get the desired output:
programming
title1 title2
Is there a way to evaluate the variable tag before the assignment? After reading a similar question I tried site.tags.{{tag}} but it didn't work.
site.tags[tag] might be what you're looking for.
Is it possible to validate if a link (or string) matches with any valid Jekyll permalink?
For example, I'm generating dynamically a navigation bar like this:
<li class="active">
About
</li>
Now, is it possible to know if "{{ base }}/{{ site.locale }}/about/the-project/" is a valid permalink ?
The idea is to skip that 'LI' tag if the link does not match with any valid permalink.
So, I'm using basically what I have for my sitemap.
{% assign tmp_posts = site.posts | where:'locale', site.locale %}
{% assign tmp_buildlogs = site.buildlog | where:'locale', site.locale %}
{% assign all_posts = tmp_posts | concat: tmp_buildlogs %}
Then, I can just check the links.
I am working with Jekyll and I have the string balh blah blah&garbage **&*&% garbage <h1>TITLE</h1> &^*$%"
Is there a way to grab TITLE? I have looked at the functions here but I don't see something that I can use.
split to the rescue !
{% assign str = 'garbage <h1>TITLE</h1> moregarbage' %}
{% assign a = str | split: '<h1>' %}
We now have garbage in a[0] and TITLE</h1> moregarbage in a[1]
{% assign b = a[1] | split: '</h1>' %}
We now have TITLE in b[0] and moregarbage in b[1]
I know this is ancient, but for anyone else coming across it: https://shopify.github.io/liquid/basics/operators/
contains contains checks for the presence of a substring inside a
string.
{% if product.title contains "Pack" %} This product's title contains
the word Pack. {% endif %}
contains can also check for the presence of a string in an array of
strings.
{% if product.tags contains "Hello" %} This product has been tagged
with "Hello". {% endif %}
contains can only search strings. You cannot use it to check for an
object in an array of objects.