Using {{ content }} and generators in Jekyll index pages - ruby

In Jekyll, a post is displayed in its layout thusly:
{{ content }}
Any generators that might have run on that content is displayed like so:
{{ content | toc_generate }}
Unfortunately, this does not work on index pages, as {{ content }} displays nothing on index pages. Instead, we are told to use a for loop:
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
So, the question:
How can I get a generator to run on an index page, since I can't use {{ content }}?
My best guess...
{% for post in site.posts %}
{{ post | toc_generator }}
{% endfor %}
...does nothing.
Any help?

Try this :
{% capture content %}
{% for post in site.posts %}
<h2>{{ post.tile }}</h2>
{{ post.content }}
{% endfor %}
{% endcapture %}
{{ content | toc_generator }}
{{ content }}

Related

Loop through unspecified folders for images in jekyll

I have used this answer https://stackoverflow.com/a/34783367/1279102; but am unable to work out how to use a variable folder.
{% for image in site.static_files %}
{% if image.path contains '{{ page.gallery }}' %}
<img src="{{ site.baseurl }}{{ image.path }}" alt="image" />
{% endif %}
{% endfor %}
I have added 'gallery' to the front matter of my post. I have tried using the absolute path, and the relative path.
It appears that no matter how you add the {{ page.gallery }} variable in the if portion, it is not changed to the corrected value.
What am I missing?
you can't interpolate within liquid tags.
assign the value to the variable beforehand.
{% capture ipath %}{{ page.gallery }}{% endcapture %}
{% for image in site.static_files %}
{% if image.path contains ipath %}
<img src="{{ site.baseurl }}{{ image.path }}" alt="image" />
{% endif %}
{% endfor %}

Getting the validation group of an erroneous form field in Twig

When looping through my form errors in Twig:
{% for error in form.vars.errors %}
{{ dump(error) }}
{% endfor %}
How can I determine the validation groups of each constraint that is failing?
Figured this out shortly after posting, I needed error.cause.constraint.groups. Like so:
{% for error in form.vars.errors %}
{% for group in error.cause.constraint.groups %}
{{ group }}
{% endfor %}
{% endfor %}

phalconphp for loop with numeric on volt

Help to fix my code in phalconphp with volt I think so long
{% for casing in casings %}
{% set i = 0 %}
{% for case in cases if case.casing == casing.type %} //it have 4 loop but cases length is 20
{% set i += 1 %}
{% endfor %}
{% if i == 0 %}
No values set
{% else %}
<ul>
{% for case in cases if case.casing == casing.type %} // it repeat twice in my code
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
<ul>
{% endif %}
{% endif %}
I know have loop.length, loop.index in for loop but when I use if in loop it have not number I want.
{% for casing in casings %}
{% if loop.first %}
<ul>
{% endif %}
{% for case in cases if case.casing == casing.type %}
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
{% if loop.last %}
<ul>
{% endif %}
{% else %}
No values set
{% endfor %}
{% for casing in casings %}
{% if loop.first %}
<ul>
{% endif %}
{% for case in cases if case.casing == casing.type %}
<li> {{ case.description }} - {{ case.value }} </li>
{% endfor %}
{% if loop.last %}
<ul>
{% endif %}
{% else %}
No values set
{% endfor %}
Thankyou for supported

Display an image for a specific Shopify variant

I'm trying to display an image in my Shopify store if a product has a variant of "OS" , if it doesn't then I need to display a different image. I also need to display the title of each variant for the product. This is what I have now, but it doesn't seem to be working.
<div class="homepage-sizechart">
<div class="sizes">
{% if product.variants.title contains 'OS' %}
{{ 'onesize-triangle.png' | asset_url | img_tag }}
{% else %}
{{ 'size-triangle.png' | asset_url | img_tag }}
{% endif %}
{% for variant in product.variants %}
<span class="{{ variant.title }}-product {% if variant.inventory_quantity == 0 %}outofstock{% endif %}">{{ variant.title }}</span>
{% endfor %}
</div>
</div>
product.variants is a collection, so you'll need to loop through it to determine if one of the variants has a title containing 'OS'.
Something like this:
{% assign contains_os = false %}
{% for variant in product.variants %}
{% if variant.title contains 'OS' %}
{% assign contains_os = true %}
{% endif %}
{% endfor %}
<div class="homepage-sizechart">
<div class="sizes">
{% if contains_os %}
{{ 'onesize-triangle.png' | asset_url | img_tag }}
{% else %}
{{ 'size-triangle.png' | asset_url | img_tag }}
{% endif %}
{% for variant in product.variants %}
<span class="{{ variant.title }}-product {% if variant.inventory_quantity == 0 %}outofstock{% endif %}">{{ variant.title }}</span>
{% endfor %}
</div>
</div>

Nested liquid loops in a Jekyll archive page not working. Using an outer loop variable inside the inner's condition

I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
{% for post in site.categories[{{ category }}] %}
<li> {{ post.title }}</li>
{% endfor %}
&#8617
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> {{ post.title }}</li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
When you do for category in site.categories ,
category[0] will give you the category name,
category[1] will give you the list of posts for that category.
That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
{% for category in site.categories %}
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
{% for post in category[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
<p>↩</p>
{% endfor %}
I've taken the liberty of fixing some markup issues - I've added <ul>...</ul> around the post link list, a <p> around the last link, a semi-colon after the 8617, and also fixed the id at the top (was missing the -ref part).
Regards!
How about...
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
<ul>
{% for post in site.posts %}
{% if post.category == category[0] %}
<li> {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
&#8617
{% endfor %}
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from for post in site.posts to endfor onto a single line.]
{% for post in site.categories.category %}
- OR -
{% for post in site.categories.category[0] %}
Also, I'm not sure why kshep's example doesn't work...

Resources