Liquid + Jekyll, "unless" not working inside "for" loop - jekyll

I'm making JSON given a set of pages. I want to skip any pages without titles, and the last element can not have a comma after it, that's bad JSON. Tried different variations, here is an example:
---
---
[
{% for page in site.pages %}
{% unless page.title %}
{% continue %}
{% else %}
{
"title":"{{ page.title }}",
"content":"{{ page.content | strip_html | strip_newlines }}",
"href":"{{ page.url }}"
}
{% endunless %}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
The end of the resulting JSON file looks like this:
{
"title":"Test Search",
"content":" ",
"href":"/search.html"
}
,
]
How do I get rid of the trailing comma? Thank you in advance.

I think your problem is that your last loop iteration is one that has no title.
Try to prepend the comma. That way you don't have to look in the future:
{% assign isFirst = true %}
{% for page in site.pages %}
{% unless page.title %}{% continue %}{% endunless %}
{% unless isFirst %},{% endunless %}
{% assign isFirst = false %}
{
"title": {{ page.title | jsonify }},
"content": {{ page.content | strip_html | strip_newlines | jsonify }},
"href": {{ page.url | jsonify }}
}
{% endfor %}
Edit: You should also use the jsonify filter to ensure proper escaping of quotes and other characters.

Try using {% raw %} to escape your non-liquid brackets, this can cause Jekyll to misbehave without throwing any errors.
If that still leads to odd behavior try creating a seperate array like the following example, which is very similar to the case from OP.
{% assign suggestions = "" | split: ',' %}
{%- for item in site[page.collection] -%}
{%- unless item.url == page.url -%}
{% assign suggestions = suggestions | push: item %}
{%- endunless -%}
{%- endfor -%}
<script id="suggest-json" type="application/json">
[
{%- for item in suggestions -%}
{% assign images = item.media | where: "type", "image" %}
{% assign image = images.first %}
{% raw %}
{
{% endraw %}
"url": "{{ site.baseurl }}{{ item.url }}",
"title": {{ item.title | jsonify }},
"subject": {{ item.game | jsonify }},
"image": {{ image.thumbnail | jsonify }},
"alt": {{ image.thumbnail_alt | jsonify }}
{% raw %}
}
{% endraw %}
{%- unless forloop.last -%},{%- endunless -%}
{%- endfor -%}
]
</script>
The problem may be that Jekyll doesn't actually consider the forloop.index as last because it isn't technically. You can avoid this by creating a new array either with a loop or the 'where' tag (loop used above).
The forloop.last should now behave properly, hope this helps.

Related

JSONIFY Filter Odd Behavior in Jekyll

Goal
Display JSON similar to this from links that have the same category minus the current page.
"relatedLink": [ "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html" ],
Code
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
{{ post.url | relative_url | prepend: site.url | replace:" ", "," | jsonify }}{% endif %}{% if forloop.last %}]{% else %}{% endif %}
{% endfor %}{% endfor %}{% endcapture %}
"relatedLink": {{ results }},
Results Error in JSON-LD
"relatedLink": [ "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" ],
jsonify did not comma separate the values in the array.
I think this is expected behavior. You're calling jsonify on post.url. jsonify converts Hashs or Arrays to JSON, not strings.
The best way to get what you want is probably this:
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
"{{ post.url | relative_url | prepend: site.url }}"{% unless forloop.last %},{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}]
{% endcapture %}
"relatedLink": {{ results }}

jekyll: Is it possible to use a page.variable, as an operator inside a conditional if statement?

JSON file in path: _data/integers.json which looks like this:
{
"100": [
{
"value": "true"
}
]
}
In Jekyll page:
---
integers:
- 100
- 200
---
What I'm trying to do:
{% assign json = site.data.integers %}
{% for integer in page.integers %} // loop
{% if json.{{ integer }}[0].value == "true" %} ... {% endif %}
{% endfor %}
e.g. use the {{ integer }} (aka page.integer[0]) as an operator inside the conditional statement.
Is there a method? ... asking for a friend.
If we keep your json and page.integers as is :
{% assign json = site.data.integers %}
{{ json | inspect }}
{% for integer in page.integers %}
{% comment %} Here we cast our integer to a string,
as json keys are strings
(100 | append:"" => "100")
{% endcomment %}
{% assign intAsStr = integer | append:"" %}
{% comment %} as a possible json[intAsStr] returns an array,
we retrieve the first and only element in it
{% endcomment %}
{% assign data = json[intAsStr].first %}
{% if data["value"] == "true" %}
<h1>We have a match on {{ intAsStr }}</h1>
{% endif %}
{% endfor %}
We can simplify a little with some refactoring
data/integers.json
{
"100": { "value": true }
}
jekyll page
---
integers:
- "100"
- "200"
---
{% assign json = site.data.integers %}
{{ json | inspect }}
{% for integer in page.integers %}
{% assign data = json[integer] %}
{% if data["value"] == true %}
<h1>We have a match on {{ integer }}</h1>
{% endif %}
{% endfor %}

How to get content from another page in Jekyll?

I want to display some pages of my Jekyll website in my Home page. How can I accomplish this?
Currently I am trying to do the following:
{% assign sorted = (site.pages | sort: 'order_home') %}
{% for my_page in sorted %}
{% if my_page.order_home %}
{{ my_page }}
{% endif %}
{% endfor %}
However, what happens is that content is exhibited in the following form:
{"layout"=>"page", "title"=>"Blog", "permalink"=>"/blog/", "comments"=>false, "order_nav"=>4, "order_home"=>3, "content"=>"
\n{% for category in site.categories reversed %}\n
\n {% capture category_name %}{{ category | first }}{% endcapture %}\n
\n\n
{{ category_name }}
\n \n {% for post in site.categories[category_name] %}\n
\n
{{post.title}}
\n
\n {% endfor %}\n
\n{% endfor %}\n
\n", "dir"=>"/blog/", "name"=>"posts.md", "path"=>"posts.md", "url"=>"/blog/"}
I would like to make the content appear as if it was the rendered page itself, with the {{...}} blocks correctly processed. Any idea on how to do this?
Thanks!
Just add .content to my_page. This results in the following code:
{% assign sorted = (site.pages | sort: 'order_home') %}
{% for my_page in sorted %}
{% if my_page.order_home %}
{{ my_page.content }}
{% endif %}
{% endfor %}

Shopify Liquid capture settings not outputting for some reason

I'm trying to get and display some theme settings which gets the site name and applies to end which i have setup in the schema but cant get it to display the settings.
In the promo_header.liquid snippet file i have:
{% capture promo_header_text_1 %} locale_promo_1_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_1 %} locale_promo_1_url_{{ shop.name }} {% endcapture %}
{% capture promo_header_text_2 %} locale_promo_2_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_2 %} locale_promo_2_url_{{ shop.name }} {% endcapture %}
<p>
{% if settings[promo_header_text_1] %}
{{ settings[promo_header_text_1] }}
{% endif %}
{% if settings[promo_header_text_2] %}
<span>/</span>{{ settings[promo_header_text_2] }}
{% endif %}
</p>
I have also tried to output using just for example
{{ settings.promo_header_text_1 }}
But not displaying anything either... when i debug and display for example
{{ promo_header_text_1 }}
It does return
locale_promo_1_text_website-test2
Which is correct and in the schema settings file (example based on that one above it is matching and is set in theme customisation)
{
"type": "text",
"id": "locale_promo_1_text_website-test2",
"label": "Promo 1 Text",
"default": "FREE US SHIPPING OVER $35"
}
What am I doing wrong?
For anyone else that might come across the same issue here is the solution I got working:
{% assign promo_header_text_1 = 'locale_promo_1_text_' | append:shop.name %}
and to output
{{ settings[promo_header_text_1] }}

One variable for different collections in Jekyll to use in forloop

I have several collections on my Jekyll site. I've added post navigation to one of the collections displaying a counter on each post page:
{% assign testimonials = site.testimonials %}
{% assign page_order = 1 %}
{% for node in testimonials reversed %}
{% if node.url == page.url %}
{{ page_order }} from {{ forloop.length }}
{% else %}
{% assign page_order = page_order | plus: 1 %}
{% endif %}
{% endfor %}
I would like to make this code work not only for site.testimonials, but for other collections as well. I tried to pass a variable for collections like this:
{% capture label %}{{ page.collection }}{% endcapture %}
{% assign collection = site.collections | where: "label",label | first %}
{% for node in collection reversed %}
{% if node.url == page.url %}
{{ page_order }} from {{ forloop.length }}
{% else %}
{% assign page_order = page_order | plus: 1 %}
{% endif %}
{% endfor %}
But it doesn't work. Is there any way to pass a variable for all collections in Jekyll to use in forloop in post navigation?
When you access collection with site.testimonials, you get collection's documents array.
{{ site.testimonials | inspect }}
# output >> [#<Jekyll::Document ...>, #<Jekyll::Document ...>, ...]
When you access a collection while looping over site.collection, you receive the collection's object :
{% assign collection = site.collections | where: "label",page.collection | first %}
{{ collection | inspect }}
# output >> { "output": true, "label": "collectionLabel",
"docs": [ doc1, docs2, ... ], "files": [],
"directory": "/path/to/collection",
"relative_directory": "_colectionDirectory" }
In your case, you just have to replace :
{% for node in collection reversed %}
By :
{% for node in collection.docs reversed %}