Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some help with Jekyll; no idea how to reach you #63

Closed
mig281 opened this issue Dec 10, 2021 · 5 comments
Closed

Need some help with Jekyll; no idea how to reach you #63

mig281 opened this issue Dec 10, 2021 · 5 comments

Comments

@mig281
Copy link

mig281 commented Dec 10, 2021

Dr. Sochat, I wanted to ask you a question about Jekyll, but I can't locate an email address for you. You seem to have solved the problem of the trailing JSON comma quite elegantly, but I'm having trouble applying your solution to my problem.

Would you be willing to tell me what I'm doing wrong here? I would be much obliged, because I don't want to insert any empty JSON elements either...

Here is the source of my woe, search.json:

[
{% for page in site.html_pages %}
{% unless page.search == "exclude" %}
{
"title": "{{ page.title | escape }}",
"tags": "{{ page.tags }}",
"keywords": "{{page.keywords}}",
"url": "{{ page.url | remove: "/"}}",
"summary": "{{page.summary | strip }}"
}
{% unless forloop.last and site.posts.size < 1 %},{% endunless %}
{% endunless %}
{% endfor %}

{% for post in site.posts %}

{
"title": "{{ post.title | escape }}",
"tags": "{{ post.tags }}",
"keywords": "{{post.keywords}}",
"url": "{{ post.url | remove: "/" }}",
"summary": "{{post.summary | strip }}"
}
{% unless forloop.last %},{% endunless %}
{% endfor %}

]

Thank you in advance for any help!

@vsoch
Copy link
Owner

vsoch commented Dec 10, 2021

You can certainly reach me on a GitHub issue without needing my email!

For trailing commas in json lists I usually just do:

{% if forloop.last %}{% else %},{% endif %}

and you shouldn't need to check if the posts < 1, if that's the case it would == 0 and wouldn't even enter the loop.

@mig281
Copy link
Author

mig281 commented Dec 10, 2021

@vsoch (My apologies. I wasn't sure if it was appropriate to open an issue not directly related to a project.)

Alas, what you propose doesn't seem to be working for me (although your suggestion about checking the number of posts makes perfect sense—I don't even have any posts; it's all pages). Unfortunately, the following still doesn't work and I get the trailing comma...

Do you think I need to resort to the complex hack you describe in your post?

[
{% for page in site.html_pages %}
{% unless page.search == "exclude" %}
{
"title": "{{ page.title | escape }}",
"tags": "{{ page.tags }}",
"keywords": "{{page.keywords}}",
"url": "{{ page.url | remove: "/"}}",
"summary": "{{page.summary | strip }}"
}
{% if forloop.last  %}{% else %},{% endif %}
{% endunless %}
{% endfor %}

]

@vsoch
Copy link
Owner

vsoch commented Dec 10, 2021

Can you throw your code (and empty posts) somewhere on GitHub so I can take a look and reproduce?

@mig281
Copy link
Author

mig281 commented Dec 11, 2021

You can fork the whole repo here: https://github.com/Qumulo/docs/
The unmodified code is here, in situ: https://github.com/Qumulo/docs/blob/gh-pages/search.json

@vsoch
Copy link
Owner

vsoch commented Dec 11, 2021

Yeah this is the problem I ran into where the unless is biting you because the forloop counter doesn't matter - you can't be sure that you're at the last one (so the last one doesn't render and you get a comma from the previous).

I would do a derivation of what I describe here - https://vsoch.github.io/2019/jekyll-lists/ you bascailly need to assemble all your things to render first, and then have one clean loop where you can be sure every item is followed by a comma (and then not at the end).

@vsoch vsoch closed this as completed Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants