Jekyll Liquid Errors are puzzling me - jekyll

I have been trying to create a site with Github and I'm new to the whole thing. I have been struggling with a piece of code that has been giving me some headache:
{% for item in site.static_files %}
{% if item.path contains '/archive' %}
{% if item.path contains 'index.html' == false %}
{% assign split_path = item.path | split: '/' %}
<p>{{item.path}}</p>
{% assign filename = split_path.last %}
<p>{{ filename }}</p>
{% endif %}
{% endif %}
{% endfor %}
This generates the following error:
Error:
Liquid Warning: Liquid Syntax Error (line 5): Expected end_of_string but found comparison in "item.path contains 'index.html' == false" in archive/index.html
Can anyone help?

Replace:
if item.path contains 'index.html' == false
With:
unless item.path contains 'index.html'
Liquid's getting confused.

Related

How to remove empty line in Jinja (exporting from readwise to logseq)

I am trying to export highlights from readwise to logseq with correct page properties.
However for some pages an empty line appears which prevents all properties to appear correctly.
How can I fix this?
This is the current jinja code:
author:: [[{{author}}]]\
full-title:: "{{full_title}}"\
category:: #{{category}}\
if url %}url:: {{url}}{% endif %}\
if document_note %}document_note:: {{document_note}}{% endif %}\
if document_tags %}tags:: {% for tag in document_tags %}#[[{{tag}}]] {% endfor %} {% endif %}
if image_url %}![]({{image_url}}){% endif %}`
ine issue for example: If document note is empty it still creates a line for that property
I managed to fix it:
author:: [[{{author}}]]\
full-title:: "{{full_title}}"\
category:: #{{category}}\
{% if url %}url:: {{url}}{% endif -%}\
{% if document_note %}document_note:: {{document_note}}{% endif -%}
{% if document_tags %}tags:: {% for tag in document_tags %}#[[{{tag}}]] {% endfor %} {% endif %}\
{% if image_url %}![]({{image_url}}){% endif %}
Changes include adding - before the % in the endif

Jinja2 appending items to a list in a for loop

I need to build a list of urls to some static images in my html. I am using jinja templates and as I understand this should work -
{% set urlList=[] %}
{% for i in data['a-list-of-tuples'] %}
{% urlList.append(url_for('static', filename='sample_out/segmented/' + i[0])) %}
{% endfor %}
But I get the error - jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'urlList'
However this works -
{% set urlList=[] %}
{% for i in data['a-list-of-tuples'] %}
{{ urlList.append(url_for('static', filename='sample_out/segmented/' + i[0])) }}
{% endfor %}
I am not sure what is happening here, can someone shed some light on this please?

jinja2 using {if} within {for}

I'm trying to make a template which iterates over a list to check if a string exists. if it does then something happens, if the string isn't in the list, something else should happen. The issue is the result is repeated for all the lines in the list, which i do not want.
e.g.
The list is some simple yaml
LIST:
- A
- B
- C
Jinja looks like
{% for line in mylist %}
{% if 'A' in line %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}
{% endfor %}
So when 'A' matches i get this
A Was found in the list
A Was not found
A Was not found
And when 'A' does not match i get this:
A Was not found
A Was not found
A Was not found
Basically i need to stop it iterating over the list and just do one thing if it matches or one thing if it doesn't match
So if 'A' matches i need it to just do
A was found
If 'A' doesn't match i need it to just do A was not found
Use some kind of flag variable:
{% set ns = namespace(found=false) %}
{% for line in mylist %}
{% if 'A' in line %}
{% set ns.found=true %}
{% endif %}
{% endfor %}
{% if ns.foo %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}

Checking grains out of list with saltstack

I want to check if a specific version of a program is already installed.
Therefore, I got a state file:
{% set rvs = ['1113','1278'] %}
{% for rv in rvs %}
{% if ('r{{ rv }}' not in grains.get('cat12', [])) %}
... install it ...
{% else %}
... do nothing ...
{% endif %}
{% endfor %}
In my grains I have:
cat12:
- r1113
I would expect that '1278' is installed and list item '1113' triggers nothing, but even that is installed again...
There's no such syntax as using {{ and }} inside {% and %}. What's inside {% and %} is already Jinja. Just concatenate the string literal and the string variable with the ~ operator.
{% if 'r' ~ rv not in grains.get('cat12', []) %}
Or you can use the format filter:
{% if 'r%s'|format(rv) not in grains.get('cat12', []) %}

Check for existence of file using Jekyll

How can I use Jekyll to test for the existence of a file?
To clarify, I want to run an {% if %} statement to check if an image file exists with the same name as the page I am on.
On my page in the YAML front matter:
----
reference-design: true
----
In my layout:
{% if page.reference-design %}
{% assign filename = page.path | remove_first: '.html' %}
<!-- How can I check if file actually exists? -->
<img src="images/reference_designs/{{ filename }}.png">
{% endif %}
As of Jekyll 2, all site files are available via site.static_files. You can use this to check if a file exists. For example:
{% for static_file in site.static_files %}
{% if static_file.path == '/favicon.ico' %}
{% assign favicon = true %}
{% endif %}
{% endfor %}
I had a similar problem to solve, but specifically looking for videos that matched the a specific directory / filename based on the markdown file.
Using file.size allowed me to test if the file (actually) exists.
{% for video in site.video-demos %}
{% assign path = page.id | replace: page.slug , "" | prepend: '/assets/media/video' | append: video.directory | append: page.slug | append: ".mp4" %}
{% assign file_exists = site.static_files | where: "path", path %}
{% if file_exists.size != 0 %}
{% include video-player.html filename = path title = video.title %}
{% endif %}
{% endfor %}
It loops through an array from my config to get part of the directory structure:
video-demos:
- title: iOS Voiceover Safari
directory: ios/
- title: Android Talkback Chrome
directory: android/
- title: Windows Jaws Chrome
directory: jaws/
- title: Windows NVDA Chrome
directory: nvda/
- title: MacOS Voiceover Safari
directory: macos/
This plugin worked for me: https://github.com/Wolfr/jekyll_file_exists
After you install it, you can use it like:
{% if page.reference-design %}
{% assign filename = page.path | remove_first: '.html' %}
{% capture img_exists %}{% file_exists {{ filename }}.png %}{% endcapture %}
{% if img_exists == "true" %}
<img src="images/reference_designs/{{ filename }}.png">
{% endif %}
{% endif %}
Read http://ecommerce.shopify.com/c/ecommerce-design/t/testing-if-a-file-exists-29624. Also you might be able to play with capture.